问题描述
我有以下情况(通过下面的命令进行验证):
- x目录中现有的python脚本文件
- x中现有的python cat
__init__.py
文件 - x已添加到
PYTHONPATH
变量中,对于登录用户和sudo
都可用
,我想用调用~/test.py
#!/usr/bin/python
import sys;print sys.path;
import mount_utils
print "Hello World!"
与sudo python ~/test.py
会导致
Traceback (most recent call last):
File "/home/richter/test.py", line 3, in <module>
import mount_utils
ImportError: No module named mount_utils
除当前目录外,PYTHONPATH
中的目录不在sys.path
中. python ~/test.py
可以正常工作(将sys.path
和Hello World!
打印到控制台). sudo -E python ~/test.py
失败,出现相同的错误.我不明白
- 如果在
sudo echo
输出中打印了PYTHONPATH
变量,为什么模块不可用 - 为什么
sudo -E
不保留环境(据我从man sudo
和 https://docs.python.org/2/using/cmdline.htmlpython bla
和sudo -E python bla
之间应该没有任何区别)
为什么会出现此错误?
详细验证情况:
$ 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: -
我从一个非常相似的未解决问题中没有任何想法为什么要忽略PYTHONPATH?
-
/etc/profile
-
/etc/profile.d/python_py.sh
-
/root/.bashrc
-
/home/richter/.bashrc
和PYTHONPATH
现在(除了上述情况)在sudo -i
提示符下的env
中可用,但在sudo env | grep PYTHONPATH
中不可用. sudo python test.py
仍然由于相同的错误而失败,即我的问题仍然存在.
export PYTHONPATH="/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib" && sudo -E python test.py
也由于相同的错误而失败.
您需要将PYTHONPATH
环境变量添加到sudoers env_keep
中.
运行:sudo visudo
(以安全的方式打开/etc/sudoers
文件).
添加此行:Defaults env_keep += "PYTHONPATH"
.
现在:wq
,并且在运行sudo
PYTHONPATH
变量I have the following situation (verfication through commands below):
- existing python script file in directory x
- existing python cat
__init__.py
file in x - x added to
PYTHONPATH
variable availble for both logged in user andsudo
and I want to invoke ~/test.py
with
#!/usr/bin/python
import sys;print sys.path;
import mount_utils
print "Hello World!"
with sudo python ~/test.py
which causes
Traceback (most recent call last):
File "/home/richter/test.py", line 3, in <module>
import mount_utils
ImportError: No module named mount_utils
the directories in PYTHONPATH
are not in sys.path
except the current directory. python ~/test.py
works as expected (prints sys.path
and Hello World!
to console). sudo -E python ~/test.py
fails with the same error. I don't understand
- Why the module isn't availble if
PYTHONPATH
variable is printed insudo echo
output - Why
sudo -E
doesn't preserve the environment (as far as I understand fromman sudo
and https://docs.python.org/2/using/cmdline.html there shouldn't be any difference betweenpython bla
andsudo -E python bla
)
Why do I get this error?
Verification of 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 didn't get any idea from the quite similar unanswered question Why is PYTHONPATH being ignored?
EDIT 1: After considering How to set environment variable for everyone under my linux system? and the fact that echo ...
is a rather bad way to ensure whether 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
is (in addition to situation described above) available in env
in sudo -i
prompt, but not in sudo env | grep PYTHONPATH
. sudo python test.py
still fails due to the same error, i.e. my problems remains.
Also export PYTHONPATH="/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib" && sudo -E python test.py
fails due to the same error.
You need to add your PYTHONPATH
environment variable to the sudoers env_keep
.
Run:sudo visudo
(which opens the /etc/sudoers
file in a safe way).
Add this line:Defaults env_keep += "PYTHONPATH"
.
Now :wq
, and that will preserve the PYTHONPATH
variable when running sudo
这篇关于当包含目录是PYTHONPATH的一部分并且文件存在时,为什么在PYTHONPATH中找不到模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!