如何告诉python3寻找合适的模块?

时间:2017-10-02 19:57:46

标签: python macos opencv matplotlib

我在stackoverflow上的第一个问题。之前我做过一些研究,人们经常遇到和我一样的问题。但到目前为止,他们的解决方案对我来说并没有成功。

我已经在我的mac上安装了自制的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;打印(sys.path中) 给了我:

>>> 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本身。我可以删除其中一些吗?并且不应该有python文件本身?

EDIT3:

所以今天我删除了所有内容并从头开始。 python3与brew numpy甚至opencv3都运行良好。     导入cv2 没有任何问题。然后我试图导入丢弃的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应该在那里,对吧?我怎么能删除这个?还有其他建议吗?

2 个答案:

答案 0 :(得分:0)

似乎Python 2在您的路径中与Python 3冲突。要解决此问题,请尝试以下答案:"This package should not be accessible on Python 3" when running python3

所有归功于Charles Duffy。

希望这有帮助!

答案 1 :(得分:0)

在bash_profile文件中添加以下行,然后注销并登录:

set PYTHONPATH=/your python deployment directory/