简体   繁体   English

从另一个virtualenv创建virtualenv

[英]Create a virtualenv from another virtualenv

Can we create a virtualenv from an existing virtualenv in order to inherit the installed libraries? 我们可以从现有的virtualenv创建virtualenv以继承已安装的库吗?

In detail: 详细地:

I first create a "reference" virtualenv, and add libraries (with versions fixed): 我首先创建一个“引用”virtualenv,并添加库(修复版本):

virtualenv ref
source ref/bin/activate
pip install -U pip==8.1.1     # <- I want to fix the version number
pip install -U wheel==0.29.0  # <- I want to fix the version number

Then: 然后:

virtualenv -p ref/bin/python myapp
source myapp/bin/activate
pip list

I get: 我明白了:

pip (1.4.1)
setuptools (0.9.8)
wsgiref (0.1.2)

How to get my installed libraries? 如何获取我安装的库?

Similar question 类似的问题

I saw a similar question: Can a virtualenv inherit from another? 我看到了一个类似的问题: virtualenv可以继承另一个吗? .

But I want a isolated virtualenv which didn't use the referenced virtualenv, except for libraries installation. 但我想要一个孤立的virtualenv,它不使用引用的virtualenv,除了库安装。 So, adding the specified directories to the Python path for the currently-active virtualenv, is not the solution. 因此,将指定的目录添加到当前活动的virtualenv的Python路径中,不是解决方案。

Why doing that? 为什么这样做?

Well, we have an integration server which builds the applications (for releases and continuous integration) and we want to keep the control on libraries versions and make the build faster. 好吧,我们有一个集成服务器来构建应用程序(用于发布和持续集成),我们希望保持对库版本的控制并使构建更快。

Create a relocatable virtualenv 创建一个可重定位的virtualenv

I think I could use a relocatable virtualenv , that way: 我想我可以使用可重定位的virtualenv ,这样:

  1. create the ref virtualenv 创建ref virtualenv
  2. make it relocatable: ``virtualenv --relocatable ref``` 让它可重新定位:``virtualenv --relocatable ref```

For "myapp": 对于“myapp”:

  • copy ref to myapp ref复制到myapp

What do you think of this solution? 您如何看待这个解决方案? Is it reliable for a distribuable release? 对于可分发的版本是否可靠?

You can solve your problem by using .pth files . 您可以使用.pth文件解决问题。 Basically you do this: 基本上你这样做:

virtualenv -p ref/bin/python myapp
realpath ref/lib/python3.6/site-packages > myapp/lib/python3.6/site-packages/base_venv.pth

After doing this and activating myapp , if you run pip list you should see all the packages from ref as well. 执行此操作并激活myapp ,如果运行pip list ,则应该看到ref所有包。 Note that any packages installed in myapp would hide the respective package from ref . 请注意, myapp安装的任何软件包都会隐藏ref的相应软件包。

当你安装第二个virtualenv时,你必须添加--system-site-packages标志。

virtualenv -p ref/bin/python myapp --system-site-packages

You may freeze list of packages from one env: 您可以从一个环境freeze包列表:

(ref) user@host:~/dir$ pip freeze > ref-packages.txt

Then install them: 然后安装它们:

(use) user@host:~/dir$ pip install -r ref-packages.txt

The pip version 1.4.1 was bundle with an old version of virtualenv . pip版本1.4.1与旧版virtualenv捆绑在一起。 For example the one shipped with Ubuntu 14.04. 例如Ubuntu 14.04附带的那个。 You should remove that from your system and install the most recent version of virtualenv . 您应该从系统中删除它并安装最新版本的virtualenv

pip install virtualenv

This might require root permissions ( sudo ). 这可能需要root权限( sudo )。

Then upgrade pip inside the virtual env pip install -U pip or recrete the env. 然后在虚拟env pip install -U pip pip内升级pip或重新创建env。

I think your problem can be solved differently. 我认为你的问题可以用不同的方式解决。 With use of PYTHONPATH . 使用PYTHONPATH First we create ref virtaulenv and install all needed packages here 首先,我们创建ref virtaulenv并在此处安装所有需要的包

$ virtualenv ref
$ source ref/bin/activate
$ pip install pep8
$ pip list
> pep8 (1.7.0)
> pip (8.1.2)
> setuptools (26.1.1)
> wheel (0.29.0)

Then we create second virtaulenv use . 然后我们创建第二个virtaulenv use

$ virtualenv use
$ source use/bin/activate
$ pip list
> pip (8.1.2)
> setuptools (26.1.1)
> wheel (0.29.0)

And now we can set our PYTHONPATH in this env to include ref's directories 现在我们可以在这个环境中设置我们的PYTHONPATH以包含ref的目录

$ export PYTHONPATH=PYTHONPATH:/home/path_to/ref/lib/python2.7/site-packages:/home/path_to/ref/local/lib/python2.7/site-packages
$ pip list
> pep8 (1.7.0)
> pip (8.1.2)
> setuptools (26.1.1)
> wheel (0.29.0)

As you see this way you just reference installed packages in ref's environment. 正如您所看到的那样,您只需在ref的环境中引用已安装的软件包。 Also note that we add this folders at the end so they will have lower priority. 另请注意,我们在末尾添加此文件夹,因此它们的优先级较低。

NOTE : this are not all folders that exists in PYTHONPATH . 注意 :这不是PYTHONPATH中存在的所有文件夹。 I included this 2 because they are main ones. 我把它包括在内,因为它们是主要的。 But if you will have some problems you can add other ones too, just lookup needed paths with this method how to print contents of PYTHONPATH 但是如果你有一些问题也可以添加其他问题,只需用这种方法查找所需的路径如何打印PYTHONPATH的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM