Python import works in interpreter, doesn't work in script Numpy/Matplotlib
我在OSX Snow Leopard上,我在我的脚本中运行2.7并且解释器似乎运行2.6
在我能够导入numpy但之后我会在尝试导入matplotlib时遇到错误,所以我去寻找解决方案并更新了我的PYTHONPATH变量,但我认为我做错了,现在简单地搞砸了一切。
这是我在我的脚本中尝试导入numpy时得到的结果:
Traceback (most recent call last):
File"./hh_main.py", line 5, in
import numpy
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/init.py", line 137, in
import add_newdocs
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/add_newdocs.py", line 9, in
from numpy.lib import add_newdoc
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/lib/init.py", line 4, in
from type_check import *
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/lib/type_check.py", line 8, in
import numpy.core.numeric as _nx
File"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/core/init.py", line 5, in
import multiarray
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/core/multiarray.so, 2): Symbol not found: _PyCapsule_Import
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/core/multiarray.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/core/multiarray.so
这是我从解释器中的sys.path得到的:
['', '/Users/joshuaschneier/Documents/python_files', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload']
这是我的PYTHONPATH,我想我更新错了:
:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
谢谢你的帮助。
你通常需要为你使用的每个版本的python安装一次numpy,matplotlib等,因为它会将自己安装到特定的'python2.x / site-packages'目录。
以上输出是从2.6或2.7会话生成的吗? 如果它是2.6会话,则是,将PYTHONPATH指向2.7将无法工作 - numpy包括已编译的C代码(例如multiarray.so文件),它将针对特定版本的python构建。
如果您不想维护两套软件包,我建议为版本2.7安装numpy,matplotlib等,删除PYTHONPATH设置,并确保脚本和解释器会话都使用2.7版。
如果你想保留两个版本,你只需要安装两个包(你可能仍然想要撤消你的PTYHONPATH更改)