简体   繁体   English

安装的Python模块上的ImportError

[英]ImportError on installed Python modules

I am trying to use a virtualenv to use opencv with python on OSX. 我正在尝试使用virtualenv在OSX上将python与opencv一起使用。 I created a virtualenv with 我创建了一个virtualenv

virtualenv --system-site-packages venv

I then installed numpy and opencv with 然后我用以下命令安装了numpy和opencv

pip install numpy opencv-python

However when I run my code using the virtualenv I get an error from the imports: 但是,当我使用virtualenv运行代码时,我从导入中得到了一个错误:

import cv2
import numpy as np

I get this error: 我收到此错误:

ImportError: No module named cv2

I haven't been able to find any information on this issue, but I can't seem to import any third party modules. 我尚未找到有关此问题的任何信息,但似乎无法导入任何第三方模块。 It doesn't work with pandas, scipy, etc. either. 它也不适用于熊猫,scipy等。 Originally I was not using a virtualenv, but the other sources I found told me to try one. 最初我没有使用virtualenv,但是我发现的其他资源告诉我尝试一个。

It looks like you have installed those modules in your default/system interpreter, not into the venv you just made. 看起来您已经在默认/系统解释器中安装了这些模块,而不是刚刚创建的venv中。 You need to switch into (activate) the venv first, before installing the modules. 在安装模块之前,您需要先切换到(激活)venv。 Activate with a command something like (depending on your project's venv location): 使用类似以下命令的命令激活(取决于项目的venv位置):

source my_project/bin/activate

See: https://docs.python.org/3/library/venv.html 参见: https : //docs.python.org/3/library/venv.html

The following works for me: 以下对我有用:

$ virtualenv --system-site-packages venv
$ source venv/bin/activate
(venv) $ pip install numpy opencv-python
(venv) $ python
>>> import numpy as np

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM