Python 如何将全局安装的包导入到 virtualenv 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13992214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:06:52  来源:igfitidea点击:

How to import a globally installed package to virtualenv folder

pythonubuntuvirtualenv

提问by Richard Knop

So I have a virtualenv folder called venv for my python project.

所以我的 python 项目有一个名为 venv 的 virtualenv 文件夹。

I can run:

我可以跑:

venv/bin/pip install -r requirements.txt

Which installs all requirements I need for the project except one, M2Crypto. The only way to install it is through apt-get:

它安装了我需要的项目的所有要求,除了一个,M2Crypto。安装它的唯一方法是通过 apt-get:

apt-get install python-m2crypto

How can I then add this package installed through apt to venv folder?

然后如何将通过 apt 安装的这个包添加到 venv 文件夹?

采纳答案by Richard Knop

What I did after all:

毕竟我做了什么:

cp -R /usr/lib/python2.7/dist-packages/M2Crypto /home/richard/hello-project/venv/lib/python2.7/site-packages/
cp -R /usr/lib/python2.7/dist-packages/OpenSSL /home/richard/hello-project/venv/lib/python2.7/site-packages/

回答by Amber

venv/bin/pip install -I M2Crypto

The -Iforces it to also be installed into the virtualenv, even if it's already globally installed.

-I强制它也可以安装到virtualenv中,即使它已经在全球安装。

回答by Corey Goldberg

--system-site-packages

gives access to the global site-packages modules to the virtual environment.

允许访问虚拟环境的全局站点包模块。

you could do:

你可以这样做:

$ sudo apt-get install python-m2crypto
$ virtualenv env --system-site-packages

... and you would then have access to m2crypto(along with all other system-wide installed packages) inside your virtualenv.

...然后您将可以访问m2crypto(以及所有其他系统范围内安装的软件包)在您的 virtualenv 中。

回答by Will

toggleglobalsitepackageswill toggle access to the system-wide site-packages.

toggleglobalsitepackages将切换对系统范围的访问site-packages

Note:You need to pip install virtualenvwrapperto get this command; the vanilla virtualenvdoesn't include it. With virtualenvwrapperyou also get the very useful mkvirtualenvand rmvirtualenvcommands, among others.

注意:你需要pip install virtualenvwrapper得到这个命令;香草virtualenv不包括它。随着virtualenvwrapper你也得到了非常有用mkvirtualenvrmvirtualenv命令,等等。

回答by roshnet

Real simple solution.

真正简单的解决方案。

In the virtual environment directory, edit the file pyvenv.cfg. Set the parameter include-system-site-packages = true, and save the file. The globally installed modules will appear the next time you activate (source venv/bin/activate) your environment.

在虚拟环境目录中,编辑文件pyvenv.cfg. 设置参数 include-system-site-packages = true,并保存文件。全局安装的模块将在您下次激活 ( source venv/bin/activate) 环境时出现。

It can be verified via pip list.

可以通过验证pip list

Enjoy!

享受!