15

I'm working on a project in python that uses OpenCV (2.3.1), among other libraries. So far, I just apt-get installed everything, but now I want to share my code with someone that might not have everything installed already. So, virtualenv seems like the perfect solution, but I get this.

$ python src/importcv.py # does nothing but import cv, no problems
$ virtualenv .           # create virtualenv here
$ source bin/activate    # activates this virtualenv
(p)$ python src/importcv.py
Traceback (most recent call last):
  File "src/test.py", line 1, in <module>
    import cv
ImportError: No module named cv

Was there something wrong in how I set up the virtualenv, or do I have to do some other step so it can see my opencv python bindings?

5 Answers 5

9

I use makefiles in my projects to install OpenCV inside Python virtualenv. Below is boilerplate example. It requires that you already have OpenCV bindings present for your system Python (/usr/bin/python) which you can get using something like yum install opencv-python or apt-get install python-opencv.

Make first queries system Python's cv2 module and retrieves location of installed library file. Then it copies cv2.so into the virtualenv directory.

VENV_LIB = venv/lib/python2.7
VENV_CV2 = $(VENV_LIB)/cv2.so

# Find cv2 library for the global Python installation.
GLOBAL_CV2 := $(shell /usr/bin/python -c 'import cv2, inspect; print(inspect.getfile(cv2))')

# Link global cv2 library file inside the virtual environment.
$(VENV_CV2): $(GLOBAL_CV2) venv
    cp $(GLOBAL_CV2) $@

venv: requirements.txt
    test -d venv || virtualenv venv
    . venv/bin/activate && pip install -r requirements.txt

test: $(VENV_CV2)
    . venv/bin/activate && python -c 'import cv2; print(cv2)'

clean:
    rm -rf venv

(You can copy-paste above snippet into a Makefile, but make sure to replace indentations with tab characters by running sed -i s:' ':'\t':g Makefile or similar.)

Now you can run the template:

echo "numpy==1.9.1" > requirements.txt
make
make test

Note that instead of symbolic link, we actually copy the .so file in order to avoid problem noted here: https://stackoverflow.com/a/19138136/1510289

Sign up to request clarification or add additional context in comments.

3 Comments

Why use awk and sed to extract the library path when python can give you the path directly, e.g. with print(cv2.__file__)? Or even better with inspect?
Thanks @leosh, you're right, much cleaner than awk and sed. I made the appropriate change to my answer.
This answer saved my day. I was struggling because cv2 was usable with python3.8 but not inside the venv. In my case I simple copied the cv2 directory from /usr/lib/python3/dist-packages/cv2/ to <venv-path>/lib/python3.8/site-packages/cv2
7

Simply copy of the cv2*.so file to the site-packages folder of the virtual environment. For example:

cp /usr/lib/python3.6/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so ~/your_virt_env_folder/YOUR_VIRT_ENV_NAME/lib/python3.6/site-packages/

Comments

6

Virtualenv creates a separate python environment. You will need to re-install all of your dependencies. EDIT it's true pip does not seem to play well with opencv. The missing module error can be resolved by copying cv shared object to your virtualenv. More info in the question linked below.

3 Comments

That's what I was afraid of. It doesn't seam like pip installation is a popular route for openCV users, see this unanswered question: stackoverflow.com/q/11184847/34910
I've added a potential solution to that question you referenced. Basically, copying the shared objects produced from the opencv installation to my virtual environment, seemed to do the trick.
Which question linked where?
3

If you want a more portable solution (no hard-coded paths, no awk on output strings), you could use the following shell snippet (after activating the venv):

echo "Importing opencv library from host into venv..."
# Find cv2 library for the global Python installation.
GLOBAL_CV2=$(/usr/bin/python3 -c 'import cv2; print(cv2.__file__)')
# Find site-packages directory in the venv
VENV_SITEPACKAGES_DIR=$(python3 -c 'import site; print(site.getsitepackages()[0])')
# Copy host-installed library file into venv
cp ${GLOBAL_CV2} ${VENV_SITEPACKAGES_DIR}

Comments

3

Try to install opencv-python inside your venv, the above should work:

pip install opencv-python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.