$PYTHONPATH 根本不工作

$PYTHONPATH 根本不工作

我通过这样的$PYTHONPATH方式永久添加.bashrc

export PYTHONPATH=$PYTHONPATH:/path/to/python-sdk

现在,当我启动 python 时,它不会显示在 sys.path` 中,并且无法导入库。我知道我可以像这样在 python 中添加它,但我希望它被永久添加。

我使用的是 Linux Mint 19.2、内核 Linux 4.15.0-54-generic 和 Python 2.7.15+。

编辑:不幸的是,另一个主题对我没有帮助,因为即使我正在使用$PYTHONPATH,我也无法导入位于定义路径中的包。我想永久添加这条路径,通常它是这样工作的,但$PYTHONPATH似乎根本不起作用。

编辑2:这是命令行,当我尝试使用时$PYTHONPATH

$ export export PYTHONPATH=$PYTHONPATH:/opt/nao/python-sdk/lib/python2.7/site-packages
$ python
Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import naoqi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named naoqi
$ sudo ls /opt/nao/python-sdk/lib/python2.7/site-packages/             
allog.py   almathswig.py      inaoqi.pyc  naoqi.py   README
allog.pyc  _almathswig.so     _inaoqi.so  naoqi.pyc  vision_definitions.py
_allog.so  expressiveness.py  motion.py   qi
almath.py  inaoqi.py          motion.pyc  _qi.so

答案1

我正在运行 Mint 19.3 和内核版本 4.15.0-106-generic - 所以离你不远。

(python3-venv) me@mycomp:chap10-Batteries_Included$ cat/etc/*release
DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=19.3
DISTRIB_CODENAME=tricia
DISTRIB_DESCRIPTION="Linux Mint 19.3 Tricia"
...
(python3-venv) me@mycomp:chap10-Batteries_Included$ uname -r
4.15.0-106-generic

在Python 3虚拟环境中运行。我正在研究 Magnus Lie Hetland 所著的《Python入门,从新手到专业人士》,并已读到第 10 章,其中讨论了这一点。当我第一次进入 shell 时,我发现没有任何分配给$PYTHONPATH以下任何一个:

(python3-venv) me@mycomp:chap10-Batteries_Included$ echo $PYTHONPATH
 
(python3-venv) me@mycomp:chap10-Batteries_Included$

因此,我按照网络上和书中各处的建议进行操作($PWD作为我的工作目录,恰好是我想要添加的目录):

(python3-venv) me@mycomp:chap10-Batteries_Included$ export PYTHONPATH=$PYTHONPATH:$PWD
(python3-venv) me@mycomp:chap10-Batteries_Included$ echo $PYTHONPATH
:/home/me/Documents/CodingProjects/python/Beginning-Python/book/chap10-Batteries_Included
(python3-venv) me@mycomp:chap10-Batteries_Included$

就这样,它就在那里:

(python3-venv) me@mycomp:chap10-Batteries_Included$ python -O
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, pprint
>>> pprint.pprint(sys.path)
['',
 '/home/me/Documents/CodingProjects/python/Beginning-Python/book/chap10-Batteries_Included',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/me/python3-venv/lib/python3.6/site-packages']
>>> 

我注意到,如果我没有在这里获得 50 分,我就会评论说,您export export PYTHONPATH=$PYTHONPATH:....只需要export一次。您也可以将其添加export PYTHONPATH=...到您的帐户中~/.profile,使其成为永久帐户。希望这对您有所帮助。

相关内容