如何复制virtualenv

问题:如何复制virtualenv

我有一个现有的virtualenv,其中包含很多软件包,但是旧版本的Django。

我想要做的就是复制此环境,因此我有另一个环境,它们的软件包完全相同,但是 Django的更新版本。我怎样才能做到这一点?

I have an existing virtualenv with a lot of packages but an old version of Django.

What I want to do is duplicate this environment so I have another environment with the exact same packages but a newer version of Django. How can I do this?


回答 0

最简单的方法是使用pip生成需求文件。需求文件基本上是一个文件,其中包含要安装(或在pip生成文件的情况下已经安装)所有python软件包的列表,以及它们的版本。

要生成需求文件,请进入原始的virtualenv,然后运行:

pip freeze > requirements.txt

这将为您生成requirements.txt文件。如果您在喜欢的文本编辑器中打开该文件,则会看到类似以下内容的内容:

Django==1.3
Fabric==1.0.1
etc...

现在,编辑这行Django==x.xDjango==1.3(或任何版本要在新的virtualenv安装)。

最后,激活新的 virtualenv并运行:

pip install -r requirements.txt

然后pip会自动下载并安装您requirements.txt文件中列出的所有python模块,无论您使用的是哪个版本!

The easiest way is to use pip to generate a requirements file. A requirements file is basically a file that contains a list of all the python packages you want to install (or have already installed in case of file generated by pip), and what versions they’re at.

To generate a requirements file, go into your original virtualenv, and run:

pip freeze > requirements.txt

This will generate the requirements.txt file for you. If you open that file up in your favorite text editor, you’ll see something like:

Django==1.3
Fabric==1.0.1
etc...

Now, edit the line that says Django==x.x to say Django==1.3 (or whatever version you want to install in your new virtualenv).

Lastly, activate your new virtualenv, and run:

pip install -r requirements.txt

And pip will automatically download and install all the python modules listed in your requirements.txt file, at whatever versions you specified!


回答 1

另一种选择是使用virtualenv-clone包:

用于克隆不可重定位的virtualenv的脚本。

Another option is to use virtualenv-clone package:

A script for cloning a non-relocatable virtualenv.


回答 2

virtualenvwrapper提供复制虚拟环境命令

cpvirtualenv ENVNAME [TARGETENVNAME]

virtualenvwrapper provides a command to duplicate virtualenv

cpvirtualenv ENVNAME [TARGETENVNAME]

回答 3

如果您正在使用Anaconda,则可以运行:

conda create --name myclone --clone myenv

这将复制myenv到名为的新创建的环境myclone

If you are using Anaconda you can just run:

conda create --name myclone --clone myenv

This will copy myenv to the newly created environment called myclone.


回答 4

最简单的选择是使用virtualenv-clone软件包。

要复制venv1venv2,请按照以下步骤操作:

  1. 安装virtualenv-clone在任一venv1或虚拟的虚拟环境venv_dummy。创建venv_dummy

    python -m virtualenv venv_dummy
    source venv_dummy/bin/activate
  2. 要安装virtualenv-clone

    (venv_dummy): pip install virtualenv-clone
  3. 复制venv1venv2

    (venv_dummy): virtualenv-clone venv1/ venv2/

Easiest option is using virtualenv-clone package.

To duplicate venv1 to venv2, follow these steps:

  1. Install virtualenv-clone in either venv1 or a dummy virtual environment venv_dummy. To create venv_dummy:

    python -m virtualenv venv_dummy
    source venv_dummy/bin/activate
    
  2. To install virtualenv-clone:

    (venv_dummy): pip install virtualenv-clone
    
  3. To duplicate venv1 to venv2:

    (venv_dummy): virtualenv-clone venv1/ venv2/
    

回答 5

您能不能简单地:

  • 将现有的虚拟环境目录复制到新目录
  • 更新到新的Django?

Can you not simply:

  • Copy the existing virtual env directory to a new one
  • Update to the new Django?