如何在virtualenv中仅对特定包使用--system-site-packages?
在一个理想的世界里,我希望能在一个使用了 --no-site-packages
的虚拟环境中使用 pip。
但是,OpenCV 的情况就有点复杂了。根据我目前了解到的情况,在虚拟环境中可靠地安装 OpenCV 似乎很棘手,所以我决定先把它作为系统包安装(我的目标平台是 Ubuntu,所以我用 apt 安装了 python-opencv
)。不过,我希望其他所有的东西都能通过 pip 安装到我的虚拟环境里。
为了让 OpenCV 可以在我的应用程序中使用,我在初始化虚拟环境时使用了 --system-site-packages
。
现在,当我用 env/bin/pip install -U -r requirements.txt
安装其他依赖时,有些依赖(恰好是 OpenCV 的依赖)被认为已经满足了。这并不意外,但这也不是我想要的结果。
有没有什么好的办法,让 只有 OpenCV 从系统包中可用,而其他所有的东西都从虚拟环境中加载和安装呢?
2 个回答
0
我已经安装了OpenCV,但为了在我的虚拟环境中使用这个安装,我按照以下步骤操作:
- 找到你的系统包目录。你可以参考@peterino的回答
- 接下来按照这些步骤进行:
# This is the path for my system-site-packages
$ cd /home/aadi/.local/lib/python3.6/site-packages
$ ls | grep cv`
# Here if you have open cv installed you'll find the result. For me, it was something like this
#I am attaching an image of my result'
# Then I copied both of the cv2 files i.e cv2 and opencv_python-3.4.0.12.dist-info to my virtual env site-packages.
$ cp -r opencv_python-3.4.0.12.dist-info /home/aadi/prog/python_apps/venv/lib/python3.6/site-package && cp -r cv2 /home/aadi/prog/python_apps/venv/lib/python3.6/site-packages
这里的python_apps是我创建虚拟环境的目录。然后我重新激活了我的虚拟环境,结果就是!导入cv2时没有出现任何错误。
(venv) ➜ python_apps python
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
1
在2018年,你可以很简单地通过使用 opencv-python 这个包,在一个虚拟环境中安装最新版本的OpenCV。