0

I want to modify a linux shell script used to install a python module (setup.sh) to add the folder the an environment variable. This is needed so I can run the python module from a terminal. e.g. python -m newtool.py

After some reading the method I implemented is in the setup.sh file to call a python script (addmoduletopath.py) and append the new python module folder to the PYTHONPATH env variable (using sys.path.append).

This appears to add the python module to the PYTHONPATH env variable. After setup when I run "python -m newtool.py" python finds the module path. However when i run the same command with sudo (i.e. sudo python -m newtool.py) I get the message "no module named newtool"

Any ideas?

Btw. Here is my addmoduletopath.py script:

#!/usr/bin/env python
import sys

newtool="/opt/newtool"
if (newtool not in sys.path):
        sys.path.append(newtool)
1

The reason this occurs is when you run in sudo mode your current env variables are not retained. If you run your sudo command with the -E switch it will pass your environment variable through.

Have a read of man sudo for more details

  • One note on this however, you can only change the environment on your CURRENT environment and your PARENT environments. – mdpc Mar 15 '15 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.