我在 /usr/lib/python3/dist-packages (通过 apt) 中安装了 numpy 1.8.2,并在 /usr/local/lib/python3.4/dist-packages (通过 pip) 中安装了最新版本 (1.9.2)。 这两个路径都在 sys.path 中 (按此顺序),但只有较旧的 apt 版本被导入到 python3 中。
➜ ~ sudo pip3 install --upgrade numpy
[sudo] password for naught101:
Real name of requirement numpy is numpy
Requirement already up-to-date: numpy in /usr/local/lib/python3.4/dist-packages
Cleaning up...
➜ ~ ipython3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy
In [2]: numpy.version
Out[2]: <module 'numpy.version' from '/usr/lib/python3/dist-packages/numpy/version.py'>
In [3]:
KeyboardInterrupt
In [3]: numpy.version.version
Out[3]: '1.8.2'
In [5]: import sys
In [6]: sys.path
Out[6]:
['',
'/usr/local/bin',
'/usr/lib/python3/dist-packages',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/usr/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python3.4/dist-packages',
'/usr/lib/python3/dist-packages/IPython/extensions']
(此处 python3 与 ipython3 作用相同)
我怎样才能让 python3 使用较新的版本?
我不能直接卸载 apt 版本,因为其他软件包依赖它。
答案1
这篇博客文章很好地解释了这背后的代码逻辑:python 如何查找包?
引用如下:
正如文档所解释的,sys.path 使用当前工作目录填充,后跟 PYTHONPATH 环境变量中列出的目录,后跟安装相关的默认路径,由站点模块控制。