64
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']    

如何PYTHONPATH在 bash 中获得所有输出?
为什么 echo $PYTHONPATH不能全部搞定?

4

6 回答 6

109

环境变量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-
包:/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
于 2013-04-29T01:17:39.667 回答
19

写吧:

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

于 2016-03-22T06:00:13.990 回答
10

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

python -c "import sys; print(sys.path)"
于 2015-11-18T21:49:14.657 回答
8

Python 在启动时会加载一堆值sys.path(通过字符串列表“实现”),包括:

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

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

如果您追求 的值sys.path,最好的方法是询问 Python(感谢@Codemonkey):

python -c "import sys; print sys.path"
于 2013-04-29T01:20:34.580 回答
6

你也可以试试这个:

Python 2.x:
python -c "import sys; print '\n'.join(sys.path)"

Python 3.x:
python3 -c "import sys; print('\n'.join(sys.path))"

输出将更具可读性和简洁性,如下所示:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions /2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /Library/Python/2.7/site-packages /System /Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

于 2017-07-27T19:38:53.297 回答
1

添加到@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-packages'、'/usr/ local/lib/python2.7/dist-packages/setuptools-39.1.0-py2.7.egg', '/usr/lib/python2.7/dist-packages']

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-packages', '/usr/lib/python3/dist-packages']

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

于 2019-02-23T17:33:10.080 回答