路径优先于不同安装位置的Python模块

时间:2017-07-18 14:45:50

标签: python macos matplotlib path python-module

在Mac OS Darwin内核版本16.6.0 x86_64上我安装了两个版本的matplotlib。随系统附带的那个:

> ls /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/ | grep matplotlib
matplotlib/
matplotlib-1.3.1-py2.7-nspkg.pth
matplotlib-1.3.1-py2.7.egg-info/

我最近安装了一个新的matplotlib版本:

> pip install --user -U matplotlib
> ls ~/Library/Python/2.7/lib/python/site-packages/ | grep matplotlib
matplotlib/
matplotlib-2.0.2-py2.7-nspkg.pth
matplotlib-2.0.2.dist-info/

但是,似乎始终导入matplotlib的系统版本而不是我安装的版本。

from __future__ import (print_function)
import sys
import matplotlib

print('sys.path:', sys.path)
print('matplotlib version:', matplotlib.__version__)
print('matplotlib path:', matplotlib.__path__)

打印:

sys.path: ['/my/dir/Desktop', '/my/dir/Library/Python/2.7/lib/python/site-packages/multiqc-1.0.dev0-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/spectra-0.0.7-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/click-6.7-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/PyYAML-3.12-py2.7-macosx-10.12-intel.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/simplejson-3.10.0-py2.7-macosx-10.12-intel.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/Jinja2-2.9.6-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/colormath-2.1.1-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/nose-1.3.7-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/tornado-4.5-py2.7-macosx-10.12-intel.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/MarkupSafe-1.0-py2.7-macosx-10.12-intel.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/networkx-1.11-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/backports_abc-0.5-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/certifi-2017.4.17-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/singledispatch-3.4.0.3-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/decorator-4.0.11-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/my/dir/Library/Python/2.7/lib/python/site-packages/mergevcf-1.0.1-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/PyVCF-0.6.8-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/pip-9.0.1-py2.7.egg', '/my/dir/Library/Python/2.7/lib/python/site-packages/dadi-1.7.0-py2.7-macosx-10.12-intel.egg', '/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', '/my/dir/Library/Python/2.7/lib/python/site-packages', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
matplotlib version: 1.3.1
matplotlib path: ['/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib']

如果我使用sys.path修改脚本内的路径:

sys.path.insert(0, '/my/dir/Library/Python/2.7/lib/python/site-packages')

它加载正确的版本,即2.0.2。

但是,如果shell中有export PYTHONPATH=$HOME/Library/Python/2.7/lib/python/site-packages:$PYTHONPATH变量,则它再次默认为matplotlib的旧系统版本1.3.1。

那么在安装相同模块的不同版本时,如何在脚本之外指定使用用户安装的模块版本而不是系统版本?

0 个答案:

没有答案