如何让 PYTHONPATH 进入 shell?

debian@debian:~$ echo $PYTHONPATH
/home/qiime/lib/:
debian@debian:~$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/stripogram-1.5-py2.7.egg', '/home/qiime/lib',
'/home/debian', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-
dynload',   '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']

如何在 bash 中获得所有 PYTHONPATH输出?
为什么 echo $PYTHONPATH不能得到所有这些?

271649 次浏览

环境变量 PYTHONPATH实际上只是添加到 Python 搜索模块的位置列表中。你可以像这样在终端打印出完整的列表:

python -c "import sys; print(sys.path)"

或者,如果想要 UNIX 目录列表样式的输出(由 :分隔) ,可以这样做:

python -c "import sys; print(':'.join(x for x in sys.path if x))"

它会输出这样的东西:

/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/
python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us
r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib
/python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist-
packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u
sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:
/usr/lib/pymodules/python2.7

Python 在启动时将大量值加载到 sys.path(通过字符串列表“实现”) ,其中包括:

  • 各种硬编码的地方
  • $PYTHONPATH的价值
  • 可能是启动文件中的一些东西(我不确定 Python 是否有 rcfiles)

$PYTHONPATH只是 sys.path最终值的一部分。

如果你想知道 sys.path的值,最好的方法是问 Python (多谢@Codemonkey) :

python -c "import sys; print sys.path"

我们这些使用 Python 3.x 的人应该这样做:

python -c "import sys; print(sys.path)"

只要写:

只要在终端中写入 which python,您就会看到正在使用的 Python 路径。

你也可以试试这个:

Python 2. x:
python -c "import sys; print '\n'.join(sys.path)" < br/> < br/> Python 3. x:
python3 -c "import sys; print('\n'.join(sys.path))" < br/>

输出将更具可读性和清晰性,如下所示:

/System/Library/Framework/Python. Framework/Versions/2.7/lib/python27.zip 系统/库/框架/Python 框架/版本/2.7/lib/python2.7 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/plat- 达尔文 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/plat-mac 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/plat-mac/lib-script 包 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/lib-tk 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/lib-old 系统/库/框架/Python 框架/版本/2.7/lib/python2.7/lib-dynload /Library/Python/2.7/site-package 系统/库/框架/Python 框架/版本/2.7/附件/lib/python 系统/库/框架/Python 框架/版本/2.7/附件/lib/python/PyObject

添加到 @ zzzzzzz 应答,我运行命令: python3 -c "import sys; print(sys.path)",它提供了不同的路径比较相同的命令与 python。用 python3显示的路径是“ python3定向的”。

查看两个不同命令的输出:

python -c "import sys; print(sys.path)"

“ ,’/usr/lib/python2.7’,’/usr/lib/python2.7/plat-x86 _ 64-linux-gnu’,’/usr/lib/python2.7/lib-tk’,’/usr/lib/python2.7/lib-old’,’/usr/lib/python2.7/lib-dynload’,’/usr/local/lib/python2.7/dist-package’,’/usr/local/lib/python2.7/dist-package-39.1.0-py2.7. egg’,’/usr/lib/python2.7/dist-package’”

python3 -c "import sys; print(sys.path)"

[”,’/usr/lib/python36.zip’,’/usr/lib/python3.6’,’/usr/lib/python3.6/lib-dynload’,’/usr/local/lib/python3.6/dist- 包’,’/usr/lib/python3/dist-包’]

这两个命令都在我的 Ubuntu 18.04机器上执行。