Why are modules in PYTHONPATH not found when the containing directory is part of PYTHONPATH and the file exists?

I have the following situation (fidelity through the commands below):

  • existing python script file in x directory
  • existing python cat __init__.py file in x
  • x is added to the PYTHONPATH variable, available for both login and sudo

and I want to call ~/test.py with

 #!/usr/bin/python import sys;print sys.path; import mount_utils print "Hello World!" 

with sudo python ~/test.py which calls

 Traceback (most recent call last): File "/home/richter/test.py", line 3, in <module> import mount_utils ImportError: No module named mount_utils 

directories in PYTHONPATH not in sys.path , except for the current directory. python ~/test.py works as expected (prints sys.path and Hello World! for the console). sudo -E python ~/test.py does not work with the same error. I do not understand

  • Why the module is unavailable if the PYTHONPATH variable is printed in sudo echo output
  • Why does sudo -E not save the environment (as far as I understand from man sudo and https://docs.python.org/2/using/cmdline.html there shouldn’t be any difference between python bla and sudo -E python bla )

Why am I getting this error?

Checking the situation in detail:

 $ env | grep PYTHONPATH PYTHONPATH=:/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib $ sudo env | grep PYTHONPATH $ LANG=C stat /home/richter/scripts/python/lib/mount_utils.py File: '/home/richter/scripts/python/lib/mount_utils.py' Size: 8374 Blocks: 24 IO Block: 4096 regular file Device: 80ch/2060d Inode: 20972052 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ richter) Gid: ( 1000/ richter) Access: 2014-08-19 17:06:37.542787417 +0200 Modify: 2014-07-31 10:54:14.709468668 +0200 Change: 2014-07-31 10:54:14.729478917 +0200 Birth: - $ LANG=C stat /home/richter/scripts/python/lib/__init__.py File: '/home/richter/scripts/python/lib/__init__.py' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 80ch/2060d Inode: 20972114 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ richter) Gid: ( 1000/ richter) Access: 2014-08-09 19:49:44.345055332 +0200 Modify: 2014-05-07 06:57:31.434851243 +0200 Change: 2014-06-25 03:17:29.569551147 +0200 Birth: - 

I got no idea from a completely similar unanswered question. Why is PYTHONPATH ignored?

EDIT 1: After reviewing How to set the environment variable for everyone on my Linux system? and the fact that echo ... is a pretty bad way to ensure that env variables are set, I put export PYTHONPATH="$PYTHONPATH:/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib" in

  • /etc/profile
  • /etc/profile.d/python_py.sh
  • /root/.bashrc
  • /home/richter/.bashrc

and now PYTHONPATH (in addition to the situation described above), available in env at the sudo -i prompt, but not at sudo env | grep PYTHONPATH sudo env | grep PYTHONPATH . sudo python test.py still fails due to the same error, i.e. my problems remain.

Also export PYTHONPATH="/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib" && sudo -E python test.py fails because the same error.

+5
source share
2 answers

You need to add the PYTHONPATH environment variable in sudoers env_keep .

Run:
sudo visudo (which safely opens the /etc/sudoers file).

Add this line:
Defaults env_keep += "PYTHONPATH" .

Now :wq and save the PYTHONPATH variable when running sudo

+6
source

Could it be that the sudo environment is running under (root), does not have the PYTHONPATH set?

 sudo echo $PYTHONPATH 

For this command, I believe that shell interpolation hits it before sudo is executed, so you basically just write

 sudo echo /home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib 

which is no different from

 echo $PYTHONPATH 

I see that you can think along these lines with sudo -E (although I could not see the specific output for this in your examples). I personally have no experience with sudo -E .

You may try

 sudo 'echo $PYTHONPATH' 

If single quotes stop interpolation as well

 sudo -E 'echo $PYTHONPATH' 

in case you use sudo -E , it really just repeats the shell interpolation problem ...

// these offers are just around the corner, your mileage may change!

+1
source

Source: https://habr.com/ru/post/1200685/


All Articles