本文介绍了当包含目录是PYTHONPATH的一部分并且文件存在时,为什么在PYTHONPATH中找不到模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况(通过下面的命令进行验证):

  • 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.pathHello World!打印到控制台). sudo -E python ~/test.py失败,出现相同的错误.我不明白

  • 如果在sudo echo输出中打印了PYTHONPATH变量,为什么模块不可用
  • 为什么sudo -E不保留环境(据我从man sudo https://docs.python.org/2/using/cmdline.html python blasudo -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?

在考虑以及echo ...是确保是否设置env变量的一种非常糟糕的方法,我将export PYTHONPATH="$PYTHONPATH:/home/richter/scripts/python:/home/richter/scripts/python/installsystem:/home/richter/scripts/python/lib"放在

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 and sudo

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 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中找不到模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:33