Linux 如何在shell中获取PYTHONPATH?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16269903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:48:53  来源:igfitidea点击:

How to get the PYTHONPATH in shell?

pythonlinuxpythonpath

提问by coder_view

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

How can I get all of PYTHONPATHoutput in bash?
Why echo $PYTHONPATHcan not get all of them?

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

采纳答案by Hubro

The environment variable PYTHONPATHis actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:

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

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

Or if want the output in the UNIX directory list style (separated by :) you can do this:

或者,如果想要 UNIX 目录列表样式的输出(以 分隔:),您可以执行以下操作:

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

Which will output something like this:

这将输出如下内容:

/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

回答by michaelb958--GoFundMonica

Python, at startup, loads a bunch of values into sys.path(which is "implemented" via a list of strings), including:

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

  • various hardcoded places
  • the value of $PYTHONPATH
  • probably some stuff from startup files (I'm not sure if Python has rcfiles)
  • 各种硬编码的地方
  • 的价值 $PYTHONPATH
  • 可能是启动文件中的一些东西(我不确定 Python 是否有rcfiles

$PYTHONPATHis only one part of the eventual value of sys.path.

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

If you're after the value of sys.path, the best way would be to ask Python (thanks @Codemonkey):

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

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

回答by zzzzzzz

Those of us using Python 3.x should do this:

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

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

回答by cjahangir

Just write:

写就好了:

just write which pythonin your terminal and you will see the python path you are using.

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

回答by george6662007

You can also try this:

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

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

你也可以试试这个:

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

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

The output will be more readable and clean, like so:

输出将更加可读和干净,如下所示:

/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

/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

回答by Guy Avraham

Adding to @zzzzzzzanswer, I ran the command:python3 -c "import sys; print(sys.path)"and it provided me with different paths comparing to the same command with python. The paths that were displayed with python3were "python3 oriented".

添加到@zzzzzzzz答案中,我运行了命令:python3 -c "import sys; print(sys.path)"与使用python. 显示的路径python3是“面向python3的”。

See the output of the two different commands:

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

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

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

['', '/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/本地/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)"

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

['', '/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']

Both commands were executed on my Ubuntu 18.04machine.

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