Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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