【发布时间】:2018-03-13 22:57:36
【问题描述】:
我在 stackoverflow 上的第一个问题。我之前做过一些研究,人们经常遇到和我一样的问题。但到目前为止,他们的解决方案对我来说并不奏效。
我已经在我的 mac 上安装了带有 homebrew 的 python3。路径在 ~/.bash_profile 中设置为
export PATH=/usr/local/bin:$PATH.
即使使用 opencv3 也能很好地工作。上周我想导入 matplotlib ......但这不起作用。
python3 以某种方式在 python2.7 中寻找它的模块。有什么建议吗?
MacBook-Pro:Desktop -----$ python3 webcam_test.py
Traceback (most recent call last):
File "webcam_test.py", line 3, in <module>
import matplotlib.pyplot as plt
File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "/usr/local/lib/python2.7/site-packages/matplotlib/colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "/usr/local/lib/python2.7/site-packages/matplotlib/artist.py", line 15, in <module>
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
File "/usr/local/lib/python2.7/site-packages/matplotlib/transforms.py", line 39, in <module>
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/matplotlib/_path.so, 2): Symbol not found: _PyCObject_Type
Referenced from: /usr/local/lib/python2.7/site-packages/matplotlib/_path.so
Expected in: flat namespace
in /usr/local/lib/python2.7/site-packages/matplotlib/_path.so
导入系统;打印(系统路径) 给我:
>>> import sys; print(sys.path)
['', '/usr/local/lib/python3', '/Users/----/Desktop', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/Cellar/matplotlib/2.0.2/libexec/lib/python3.6/site-packages', '/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python3.6/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/local/Cellar/matplotlib/2.0.2/libexec/lib/python2.7/site-packages', '/usr/local/Cellar/numpy/1.13.3/libexec/nose/lib/python2.7/site-packages', '/usr/local/opt/opencv/lib/python2.7/site-packages', '/usr/local/opt/opencv/lib/python3.6/site-packages']
>>>
对我来说似乎有点长...
编辑 2:
刚刚检查了我的 /usr/local/bin/ 。我想这有点乱。在其他文件中,每个版本都有 python2、python2.7、python3、python3.6、python3.6m 和 pythonX.X-config 的别名。但没有蟒蛇本身。我可以删除其中的一些吗?并且不应该有python文件本身吗?
编辑3:
所以今天我删除了所有内容并从头开始。 python3 与 brew numpy 甚至 opencv3 都可以正常工作。 导入简历2 完全没有问题。然后我尝试导入matplotlib,它抛出了
mport matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: bad magic number in 'matplotlib': b'\x03\xf3\r\n'
所以我检查了我的 sys.path
>>> sys.path
['', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/opt/opencv/lib/python2.7/site-packages', '/usr/local/opt/opencv/lib/python3.6/site-packages']
2.7 不应该在那里,对吧?我怎样才能删除这个?还有其他推荐吗?
【问题讨论】:
-
我没有 macbook,但在 windows 上我寻找
PATH和PYTHONPATH环境变量并将它们重新指向 python-3,因为我在同一台笔记本电脑上同时拥有 2.7 和 3.x。 -
试过了。也不起作用...但是谢谢!
-
import sys; print(sys.path)给你什么? -
必须为此写一个新答案...太长了
-
您可以点击问题下方的edit 按钮,为您的帖子添加更多信息。
标签: python macos opencv matplotlib