OpenCV and python/virtualenv?
Asked Answered
E

5

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?

Eurystheus answered 9/11, 2012 at 16:35 Comment(0)
K
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.

Kotto answered 9/11, 2012 at 16:42 Comment(3)
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: https://mcmap.net/q/454767/-running-opencv-from-a-python-virtualenv/34910Eurystheus
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.Kotto
Which question linked where?Gauger
C
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://mcmap.net/q/823238/-is-dependency-on-a-symlink-possible-in-a-makefile

Chenee answered 6/10, 2013 at 20:5 Comment(3)
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?Unblessed
Thanks @leosh, you're right, much cleaner than awk and sed. I made the appropriate change to my answer.Chenee
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/cv2Sporocyst
S
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/
Straus answered 9/7, 2019 at 11:2 Comment(0)
K
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.

Kotto answered 9/11, 2012 at 16:42 Comment(3)
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: https://mcmap.net/q/454767/-running-opencv-from-a-python-virtualenv/34910Eurystheus
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.Kotto
Which question linked where?Gauger
U
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}
Unblessed answered 15/9, 2020 at 2:23 Comment(0)
S
3

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

pip install opencv-python

Scavenge answered 14/4, 2021 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.