某些Python版本无法识别已安装的软件包

时间:2015-05-16 16:17:41

标签: python python-2.7 unix pip

我已经使用Python.org的python安装程序在我的macbook上安装了python。随后,我去安装了pip,ipython和numpy。一切似乎都很好。但是,现在我遇到了以下问题。我运行ipython时可以导入numpy,但是当我运行常规python时不能导入。

E.g。

Logister-MacBook-Pro:~ Logister$ ipython
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- 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]: import site; site.getsitepackages()
Out[2]:
['/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/Library/Python/2.7/site-packages']

然而,当我尝试在python 2.7.9中做同样的事情时:

Logister-MacBook-Pro:~ Logister$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> import site; site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', 
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python',
 '/Library/Python/2.7/site-packages']

当我尝试通过pip安装numpy时,它给出了以下响应:

Logister-MacBook-Pro:~ Logister$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade): 
numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

似乎python 2.7.6了解在哪里可以找到numpy,但2.7.9却没有。或者,我怎样才能将2.7.9指向正确的位置,或者我如何安装numpy以便2.7.9也能看到它?

编辑:我可以运行: site.addsitedir('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python') 在2.7.9然后我能够导入numpy。但是每次我发布2.7.9时我都不想这样做。有永久性修复吗?另外,如何让ipython运行2.7.9而不是2.7.6?

3 个答案:

答案 0 :(得分:0)

我有类似的问题。 您的计算机上安装了两个不同版本的python。 然后你在python 2.7.6上安装了numpy。但python 2.7.9的文件夹没有numpy。你明白了吗?

另外,你的macOS有一组名为“Environment Variables”的变量,它用python 2.7.9识别命令python。

然后,如果你想使用numpy和python的解释器你需要更改命令“python”的环境变量采用版本2.7.6或在python 2.7.9安装numpy并使用两个版本。

祝你好运!

答案 1 :(得分:0)

我能够通过在.bash_profile中添加以下行来解决问题:

export PYTHONPATH=${PYTHONPATH}:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

然后我按照this stackoverflow question.

更改了Ipython版本

答案 2 :(得分:0)

使用2个不同的版本,您的计算机上必须有2个python解释器。它们安装在具有不同库的不同位置。因此,在使用解释器B时,您无法找到安装在解释器A上的模块。您可以使用哪个命令来确定这些解释器的位置。你可以手工制作一个从一个文件夹到另一个文件夹的符号链接,但它会弄乱你的想法,你可能会在以后迷路。

我建议您通过Homebrew安装python和ipython,这样它会自动为您完成所有艰苦的工作。

安装自制程序

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

既然你说numpy,你可能正在寻找科学的东西:

# set up some taps and update brew
brew tap homebrew/science # a lot of cool formulae for scientific tools
brew tap homebrew/python # numpy, scipy, matplotlib, ...
brew update && brew upgrade

# install a brewed python
brew install python

稍后您可以投放,但我建议您关注this教程:

brew install zmq
pip install ipython[all]