OpenCV and python/virtualenv?

opencv python
pip install opencv
opencv in virtualenv
opencv download
install opencv-python 3
could not find a version that satisfies the requirement opencv-python
python 3.8 opencv
no matching distribution found for opencv-python

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?

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.

OpenCV and python/virtualenv?, 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. 2019-11-21 Update: An update has been issued to this blog post due to compatibility issues with OpenCV on the Raspberry Pi 4 running BusterOS using this pip install method. Be sure to find the updates via ctrl + f as you search for “2019-11-21 Update”. To learn how to pip install OpenCV on your system, just keep reading.

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; print(cv2)' | awk '{print $$4}' | sed s:"['>]":"":g)

# 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

Installation of Opencv, numpy, scipy inside a virtualenv, Useful for python server side deployments that need computer vision to install OpenCV inside a virtualenv without touching the global python� I am trying to install the latest version of OpenCV on RHEL6. I have python 2.7.8 in a virtualenv (following this SO question). My settings are like this:

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/

Setting up openCV with Python virtualenvs (Example), The hardest part in using openCV is installing it - especially if you want to use it within a Python virtualenv. Its been a while since I had to swear� virtualenvwrapper is a set of extensions to virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing our development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

Weblog | Pyrunner, It turns out that installing OpenCV into a Python virtual environment is pretty easy (assuming you know what you're doing). To save myself time� python virtualenv tutorial. virtualenv. python. Publish Date: 2018-09-20 our py2 virtual environment is located in our home directory — thus to use OpenCV

Installing Python, OpenCV, and other packages with virtualenv , Additionally, an exact copy of the Python or Python3 binary is placed in this directory. Once this virtual environment is activated, you can install packages in the� OpenCV on Wheels. Unofficial pre-built OpenCV packages for Python.. Installation and Usage. If you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python's site-packages), remove it before installation to avoid conflicts.

Installing OpenCV in a virtualenv on Ubuntu and OSX, This will install OpenCV, the Python bindings, and Numpy system-wide, but afterward you will be able to use them inside a virtualenv. $ unzip opencv-2.4.9.zip $ cd opencv-2.4.9 $ mkdir build $ cd build Configure the make files using cmake. There is a flag required for the Python bindings that I couldn't find in the official documentation, only in StackOverflow questions: BUILD_NEW_PYTHON_SUPPORT.

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.