[英]Python import works in interpreter, doesn't work in script Numpy/Matplotlib
I'm on OSX Snow Leopard and I run 2.7 in my scripts and the interpreter seems to be running 2.6 我在OSX Snow Leopard上,我在脚本中运行2.7,解释器似乎在运行2.6
Before I was able to import numpy but then I would get an error when trying to import matplotlib so I went looking for a solution and updated my PYTHONPATH variable, but I think I did it incorrectly and have now simply screwed everything up. 在我能够导入numpy之前,但是随后尝试导入matplotlib时出现错误,因此我一直在寻找解决方案并更新了PYTHONPATH变量,但是我认为我做错了,现在只是搞砸了所有内容。
This is what I get when I try and import numpy in my script: 这是我尝试在脚本中导入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/Framewo
追溯(最近一次通话最近):导入numpy中的文件“ ./hh_main.py”,第5行,文件“ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy” / init .py“,第137行,位于import add_newdocs文件“ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/add_newdocs.py”的第9行, numpy.lib import add_newdoc文件“ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/lib/ init .py”,第4行,来自type_check import * File“ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site->packages/numpy/lib/type_check.py“,第8行,在导入numpy.core.numeric中作为_nx文件” / Library /导入多数组ImportError中的Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site-> packages / numpy / core / init .py“,第5行,导入错误:dlopen(/Library/Frameworks/Python.framework/Versions /2.7/lib/python2.7/site->packages/numpy/core/multiarray.so,2):找不到符号:_PyCapsule_Import引用自:/ Library / Framewo rks/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
rks / Python.framework / Versions / 2.7 / lib / python2.7 / site-> packages / numpy / core / multiarray.so预期格式:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2中的平面命名空间.7 / site-> packages / numpy / core / multiarray.so
Furthermore this is what I get from sys.path in the interpreter: 此外,这是我从解释器中的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']
['','/ Users / joshuaschneier / Documents / python_files','/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages','/Library/Frameworks/Python.framework/版本/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']
And this is my PYTHONPATH which I guess I updated wrong: 这是我的PYTHONPATH,我想我更新错了:
:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
Thanks for any help. 谢谢你的帮助。
You'll generally need to install numpy, matplotlib etc once for every version of python you use, as it will install itself to the specific 'python2.x/site-packages' directory. 通常,您需要为所使用的每个版本的python安装一次numpy,matplotlib等,因为它将自身安装到特定的“ python2.x / site-packages”目录中。
Is the above output generated from a 2.6 or 2.7 session? 以上输出是从2.6或2.7会话生成的吗? If it's a 2.6 session, then yes, pointing your PYTHONPATH at 2.7 won't work - numpy includes compiled C code (eg the multiarray.so file) which will have been built against a specific version of python.
如果是2.6会话,则可以,将PYTHONPATH指向2.7将不起作用-numpy包含已编译的C代码(例如multiarray.so文件),该代码将针对特定版本的python构建。
If you don't fancy maintaining two sets of packages, I'd recommend installing numpy, matplotlib etc all for version 2.7, removing that PYTHONPATH setting, and making sure that both scripts and interpreter sessions use version 2.7. 如果您不想维护两组软件包,建议您为2.7版安装numpy,matplotlib等,删除PYTHONPATH设置,并确保脚本和解释器会话都使用2.7版。
If you want to keep both versions you'll just have to install each packages twice (and you'll probably still wnat to undo your PTYHONPATH change) 如果您想同时保留两个版本,则只需将每个软件包安装两次(并且您可能仍然会被untat撤消PTYHONPATH更改)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.