【问题标题】:python version and python sys.pathpython版本和python sys.path
【发布时间】:2016-06-14 05:21:26
【问题描述】:

我安装了 anaconda,不想使用系统 python 及其模块。

假设anaconda在独立环境下运行python。然而,

$ which python
/home/user/anaconda/bin/python

$ python --version
Python 2.7.3

$ ~/anaconda/bin/python --version
Python 2.7.11 :: Anaconda 2.5.0 (64-bit)

如你所见,python链接到2.7.3版本的系统python,虽然which表示指向anaconda python。

$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys;
>>> from pprint import pprint as p
>>> p(sys.path)
['',
 '/soft/python/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/mock-1.0.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/nose-1.3.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/pyparsing-2.0.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/tornado-3.2-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/six-1.6.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/backports.ssl_match_hostname-3.4.0.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdfgui-1.0_r6798_20120508-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.utils-1.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdffit2-1.0_r6773_20111122-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.Structure-1.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdfgetx-1.0_r2152_20130314-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/rasterstats-0.7.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/rasterio-0.24.1-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/Shapely-1.5.9-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/enum34-1.0.4-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/snuggs-1.3.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/cligj-0.2.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/affine-1.2.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/click-4.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/progress-1.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/h5py-2.5.0-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages',
 '/home/user',
 '/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/gtk-2.0',
 '/usr/lib/pymodules/python2.7']

python路径中有很多系统路径是很自然的。但是对于anaconda python来说,

$ ~/anaconda/bin/python
Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> 
>>> import sys
>>> from pprint import pprint as p
>>> p(sys.path)
['',
 '/soft/python/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/mock-1.0.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/nose-1.3.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/pyparsing-2.0.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/tornado-3.2-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/six-1.6.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/backports.ssl_match_hostname-3.4.0.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdfgui-1.0_r6798_20120508-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.utils-1.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdffit2-1.0_r6773_20111122-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.Structure-1.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/diffpy.pdfgetx-1.0_r2152_20130314-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/rasterstats-0.7.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/rasterio-0.24.1-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/Shapely-1.5.9-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/enum34-1.0.4-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/snuggs-1.3.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/cligj-0.2.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/affine-1.2.0-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/click-4.1-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/progress-1.2-py2.7.egg',
 '/soft/python/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages/h5py-2.5.0-py2.7-linux-x86_64.egg',
 '/soft/python/lib/python2.7/site-packages',
 '/home/user',
 '/home/user/anaconda/lib/python27.zip',
 '/home/user/anaconda/lib/python2.7',
 '/home/user/anaconda/lib/python2.7/plat-linux2',
 '/home/user/anaconda/lib/python2.7/lib-tk',
 '/home/user/anaconda/lib/python2.7/lib-old',
 '/home/user/anaconda/lib/python2.7/lib-dynload',
 '/home/user/anaconda/lib/python2.7/site-packages',
 '/home/user/anaconda/lib/python2.7/site-packages/Sphinx-1.3.5-py2.7.egg',
 '/home/user/anaconda/lib/python2.7/site-packages/cryptography-1.0.2-py2.7-linux-x86_64.egg',
 '/home/user/anaconda/lib/python2.7/site-packages/flake8-2.5.1-py2.7.egg',
 '/home/user/anaconda/lib/python2.7/site-packages/mccabe-0.3.1-py2.7.egg',
 '/home/user/anaconda/lib/python2.7/site-packages/setuptools-20.1.1-py2.7.egg']

还有很多系统路径。

如果我通过

过滤 sys.path

sys.path = filter(lambda (x): x.startswith('/home/user/anaconda'), sys.path) python 路径是干净的,但是会有很多 ImportErrors。

我已经导出了$PYTHONPATH=

【问题讨论】:

    标签: python python-2.7 pythonpath


    【解决方案1】:
    1. 执行type python 以检查别名(或函数)。
    2. 执行hash -r 更新$PATH 哈希表,然后再次尝试which

    一般来说,使用type 可能比使用which 更好,因为前者是内置的shell,并且可以访问shell 的PATH 哈希表、函数和别名。

    • 如果您特别想查找外部程序(如which 所做的那样),您可以使用

      type -p 
      

      也就是说,它是一个更可靠的which

    • 如果您想查找具有给定名称的可执行命令的所有位置,您可以使用

      type -a
      

    【讨论】:

    • 太棒了!!!这很棘手。 type pythonpython is aliased to 'export PYTHONPATH=/soft/python/lib/python2.7/site-packages:${PYTHONPATH};/usr/bin/python'。我正在覆盖它。但是为什么我直接运行/home/user/anaconda/bin/python还是有很多系统路径呢?
    • 我明白了。我运行pythonPYTHONPATH 被污染了。
    【解决方案2】:

    如果您想使用 Python3,请创建指向 Python 的符号链接并在基于 Debian 的 Linus 发行版上 pip。

    sudo ln -s /usr/bin/python3 /usr/bin/python
    sudo ln -s /usr/bin/pip3 /usr/bin/pip
    

    【讨论】:

      【解决方案3】:

      您是否设置并激活了 Conda 环境?

      基本上你需要在一个文件夹中创建一个项目,启动一个终端并设置 Conda 环境

      conda create -n yourenvname python=x.x anaconda
      

      在这里,您可以为您的环境选择一个名称并设置 Python 版本、anaconda 以及该环境所需的任何其他包。然后,Conda 将为您安装所有这些并创建环境。

      当你需要使用环境时,只需通过以下方式激活它:

      activate yourenvname
      

      阅读更多:https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/20/conda/

      【讨论】:

        猜你喜欢
        • 2014-06-06
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多