简体   繁体   中英

How can I use Opencv outside the python virtual environment?

So I want to use OpenCV with Python on a Ubuntu 14.04 system. To install it I followed this tutorial:

https://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/#comment-441399

When I am inside the environment that the tutorial suggests to create everything works fine. But if I try outside the environment I get the following error:

$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>>

Step 8 in your linked tutorial explains to you why the author is adding the extra step of using a virtual environment:

Step 8:

Install virtualenv and virtualenvwrapper. These two packages allow us to create separate Python environments for each project we are working on. While installing virtualenv and virtualenvwrapper is not a requirement to get OpenCV 3.0 and Python 2.7+ up and running on your Ubuntu system, I highly recommend it and the rest of this tutorial will assume you have them installed!

If you don't want to wrap it up in a virtual environment and have access to only one version of OpenCV across your entire system then basically just skip this step.

When you then get to Step 11 you can ignore the advice about having to step into the CV virtual environment because you should just have access to this from everywhere. Ignore this advice:

However, our cv virtual environment is located in our home directory — thus to use OpenCV within our cv environment, we first need to sym-link OpenCV into the site-packages directory of the cv virtual environment:

This all being said you can actually just follow the OpenCV Tutorial on the matter, which does not install it into a virtual environment. You can access that tutorial here

I found a solution, but first let me explain the problem The problem is that I have 2 Python installations. - The one I Installed - One that came with anaconda

Following other tutorials I was able to confirm that the file

cv2.so

was in fact in the directory

usr/local/lib/python2.7/site-packages

(my python installation) file that is used to link python and opnecv

But then executing the command

which python

I realize that the default python being executed was the anaconda one, that one din't have the opencv-python configuration file ie, cv2.so .

Solution: I just copied the cv2.so python-opencv configuration file form the python I installed .ie, /usr/local/lib/python2.7/site-packages directory to the python installation directory of python-anaconda .ie, /home/anaconda2/lib/python2.7/site-packages this last directory may variate based in your installation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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