OpenCV和python/virtualenv?

Nat*_*ons 6 python linux opencv virtualenv

我正在使用OpenCV(2.3.1)以及其他库中的python项目.到目前为止,我只是apt-get安装了所有内容,但现在我想与可能尚未安装所有内容的人共享我的代码.所以,virtualenv似乎是完美的解决方案,但我明白了.

$ 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
Run Code Online (Sandbox Code Playgroud)

我如何设置virtualenv有什么不对,还是我必须做一些其他步骤,以便它可以看到我的opencv python绑定?

B.K*_*cis 7

只需将文件复制cv2*.so到虚拟环境的 site-packages 文件夹中即可。例如:

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/
Run Code Online (Sandbox Code Playgroud)


Bri*_*jes 6

Virtualenv创建了一个单独的python环境.您需要重新安装所有依赖项.编辑它真正的点子似乎不适合opencv.可以通过将cv共享对象复制到virtualenv来解决丢失的模块错误.以下链接问题中的更多信息.


Vel*_*ker 5

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
Run Code Online (Sandbox Code Playgroud)

(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
Run Code Online (Sandbox Code Playgroud)

Note that instead of symbolic link, we actually copy the .so file in order to avoid problem noted here: /sf/answers/1339669551/