Python
$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
我正在執行 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 的“Beginning Python, From Novice to Professional”,並在第 10 章討論了這個問題。當我第一次進入我的外殼時,我發現沒有分配給
$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
以使其永久化。我希望這有幫助。