如何在shell中获取pythonpath?

时间:2022-07-25 23:10:56
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 pythonpath output in the bash?
Why echo $PYTHONPATH can not get all of them?

我怎样才能在bash中获得所有pythonpath输出?为什么echo $ PYTHONPATH无法获得所有这些?

5 个解决方案

#1


55  

The environment variable PYTHONPATH is 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

#2


5  

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

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

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

#3


4  

Python, at startup, loads a bunch of values into sys.path, including:

Python在启动时会将一堆值加载到sys.path中,包括:

  • various hardcoded places
  • 各种硬编码的地方

  • the value of $PYTHONPATH
  • $ PYTHONPATH的值

  • probably some stuff from startup files (I'm not sure if Python has rcfiles)
  • 可能是启动文件中的一些东西(我不确定Python是否有rcfiles)

$PYTHONPATH is 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"

#4


2  

Just write:

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

只需在你的终端中编写哪个python,你就会看到你正在使用的python路径。

#5


0  

You can also try this:

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

您也可以尝试这样:python -c“import sys; print'\ n'.join(sys.path)”

the output will be more readable and clean:

输出将更具可读性和清洁性:

/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

#1


55  

The environment variable PYTHONPATH is 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

#2


5  

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

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

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

#3


4  

Python, at startup, loads a bunch of values into sys.path, including:

Python在启动时会将一堆值加载到sys.path中,包括:

  • various hardcoded places
  • 各种硬编码的地方

  • the value of $PYTHONPATH
  • $ PYTHONPATH的值

  • probably some stuff from startup files (I'm not sure if Python has rcfiles)
  • 可能是启动文件中的一些东西(我不确定Python是否有rcfiles)

$PYTHONPATH is 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"

#4


2  

Just write:

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

只需在你的终端中编写哪个python,你就会看到你正在使用的python路径。

#5


0  

You can also try this:

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

您也可以尝试这样:python -c“import sys; print'\ n'.join(sys.path)”

the output will be more readable and clean:

输出将更具可读性和清洁性:

/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