简体   繁体   English

Python导入在解释器中起作用,在脚本Numpy / Matplotlib中不起作用

[英]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.

相关问题 `import pyspark`可在Jupyter中使用,但不适用于python shell / script - `import pyspark` works in Jupyter, but doesn't work with python shell/script import pymongo在Python解释器中工作,但不在脚本中工作 - import pymongo works in Python interpreter but not script 当Python启动器在解释器中工作时,为什么无法将脚本(带有shebang)导入到脚本中? - Why can't the Python launcher import in a script (with shebang), when it works in the interpreter? 模块导入适用于解释器,但不适用于脚本 - Module import works on interpreter but not on a script 为什么 '%matplotlib inline' 在 python 脚本中不起作用? - Why doesn't '%matplotlib inline' work in python script? Python无法在我的脚本中“导入nltk”,但在解释器中工作 - Python failed to `import nltk` in my script but works in the interpreter 即使使用相同的解释器从 shell 运行,使用 python IntelliJ 运行也不起作用 - Run using python IntelliJ doesn't work even though run from shell using same interpreter works Python 3 cgi脚本不起作用,但是python 2有效 - Python 3 cgi script doesn't work, but python 2 works Python脚本在Windows上不起作用(但适用于mac) - Python script doesn't work on Windows (but works on mac) 具有open('filename')的Python脚本可用于IDLE,但不能在控制台中使用 - Python script with open('filename') works with IDLE but doesn't work in console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM