如何将模块从一个virtualenv复制到另一个

时间:2022-09-24 18:47:55

Is it possibe to copy python modules from one virtualenv to another.If so how is this done?

是否可以将python模块从一个virtualenv复制到另一个。如果这样做怎么办?

4 个解决方案

#1


35  

As long as you're moving them from one virtualenv to another on the same machine, you could easily just do:

只要你在同一台机器上将它们从一个virtualenv移动到另一个virtualenv,你就可以轻松地做到:

$ cp -r [env1]/lib/pythonX.X/site-packages/* [env2]/lib/pythonX.X/site-packages/

However, if the environments are on different machines or utilizing different versions of python or some other major difference, it's probably not a good idea. In general it's much safer to generate a requirements.txt, and then use that to load up all the same modules in the other environment. You can create the file manually if you like, but it's easier to just use pip.

但是,如果环境在不同的机器上或使用不同版本的python或其他一些主要区别,那可能不是一个好主意。通常,生成requirements.txt然后使用它来加载其他环境中的所有相同模块会更安全。您可以根据需要手动创建文件,但只使用pip更容易。

$ pip freeze -E [env1] > requirements.txt

Or, if your virtualenv is activated already, you can simply do:

或者,如果您的virtualenv已经激活,您可以简单地执行:

$ pip freeze > requirements.txt

Then, in your other environment, you can do:

然后,在您的其他环境中,您可以执行以下操作:

$ pip install -E [env2] -r /path/to/requirements.txt

#2


2  

I am working on a 64bit machine with Ubuntu-14.04-64. I compiled and installed python-3.4.3 to /opt/python3.4/ and created a vitualenv based on this python.

我正在使用Ubuntu-14.04-64的64位机器。我编译并安装了python-3.4.3到/opt/python3.4/并基于这个python创建了一个vitualenv。

mkvirtualenv -p /opt/python3.4/bin/python venv1

Also for ease:

也方便:

sudo apt-get install virtualenvwrapper

With the venv installed and working with PyQt5 successfully (the hard bit) plus numpy, scipy, ipython etc. I installed virtualenv-clone:

随着venv安装和成功使用PyQt5(硬位)加上numpy,scipy,ipython等我安装了virtualenv-clone:

workon myvenv
pip install virtual-clone
deactivate

and then ran:

然后跑:

virtualenv-clone venv1 venv2

PyQt5 works this way. The command-line prompt still names venv1 as active but within ~/.virtualenv/venv2

PyQt5以这种方式工作。命令行提示符仍然将venv1命名为活动但在〜/ .virtualenv / venv2中

cat activate* | grep "venv1"

shows 3 entries within the three files activate, activate.csh, and activate.fish

显示三个文件中的3个条目activate,activate.csh和activate.fish

In activate, change

在激活,更改

if [ "x(myvenv1) " != x ] ; then
        PS1="(myvenv1) $PS1"
else

to

...
        PS1="(myvenv2) $PS1"
...

In activate.csh change

在activate.csh中更改

if ("venv1" != "") then
        set env_name = "venv1"
else

to

...
    set env_name = "venv2"
...

In activate.fish change

在activate.fish改变

if test -n "(venv1) "
        printf "%s%s%s" "(venv1) " (set_color normal) (_old_fish_prompt)
        return
end

to

...
    printf "%s%s%s" "(venv2) " (set_color normal) (_old_fish_prompt)
...

Now when you source ~/.virtualenv/venv2/bin/activate or workon venv2 the command prompt will correctly display your environment (the cloned copy of venv1).

现在,当您使用〜/ .virtualenv / venv2 / bin / activate或workon venv2时,命令提示符将正确显示您的环境(venv1的克隆副本)。

Edit: this doesn't answer the question "How to copy modules from one virtualenv to another" but I'm pretty sure the result is in many cases the desired one, namely the creation of a new venv based on a previously created one which includes (all of) the previously installed modules.

编辑:这不回答“如何将模块从一个虚拟复制到另一个复制”的问题,但我很确定结果在很多情况下是所需的,即基于先前创建的一个新的venv的创建包括(所有)以前安装的模块。

#3


1  

Usually you are able to copy the .egg-info from the lib/site-packages folder of the virtualenv to the lib/site-packages of the other environment.

通常,您可以将.egg-info从virtualenv的lib / site-packages文件夹复制到其他环境的lib / site-packages。

#4


1  

seems like we can't just copy one virtualenv as another one. even you chnage the $VIRTUAL_ENV in the activate file, it still act as in origin virtualenv and pip will install all the packages to origin site-packages/

似乎我们不能只将一个虚拟复制品复制为另一个。即使你在激活文件中输入$ VIRTUAL_ENV,它仍然可以作为原始virtualenv,pip将所有包安装到origin site-packages /

#1


35  

As long as you're moving them from one virtualenv to another on the same machine, you could easily just do:

只要你在同一台机器上将它们从一个virtualenv移动到另一个virtualenv,你就可以轻松地做到:

$ cp -r [env1]/lib/pythonX.X/site-packages/* [env2]/lib/pythonX.X/site-packages/

However, if the environments are on different machines or utilizing different versions of python or some other major difference, it's probably not a good idea. In general it's much safer to generate a requirements.txt, and then use that to load up all the same modules in the other environment. You can create the file manually if you like, but it's easier to just use pip.

但是,如果环境在不同的机器上或使用不同版本的python或其他一些主要区别,那可能不是一个好主意。通常,生成requirements.txt然后使用它来加载其他环境中的所有相同模块会更安全。您可以根据需要手动创建文件,但只使用pip更容易。

$ pip freeze -E [env1] > requirements.txt

Or, if your virtualenv is activated already, you can simply do:

或者,如果您的virtualenv已经激活,您可以简单地执行:

$ pip freeze > requirements.txt

Then, in your other environment, you can do:

然后,在您的其他环境中,您可以执行以下操作:

$ pip install -E [env2] -r /path/to/requirements.txt

#2


2  

I am working on a 64bit machine with Ubuntu-14.04-64. I compiled and installed python-3.4.3 to /opt/python3.4/ and created a vitualenv based on this python.

我正在使用Ubuntu-14.04-64的64位机器。我编译并安装了python-3.4.3到/opt/python3.4/并基于这个python创建了一个vitualenv。

mkvirtualenv -p /opt/python3.4/bin/python venv1

Also for ease:

也方便:

sudo apt-get install virtualenvwrapper

With the venv installed and working with PyQt5 successfully (the hard bit) plus numpy, scipy, ipython etc. I installed virtualenv-clone:

随着venv安装和成功使用PyQt5(硬位)加上numpy,scipy,ipython等我安装了virtualenv-clone:

workon myvenv
pip install virtual-clone
deactivate

and then ran:

然后跑:

virtualenv-clone venv1 venv2

PyQt5 works this way. The command-line prompt still names venv1 as active but within ~/.virtualenv/venv2

PyQt5以这种方式工作。命令行提示符仍然将venv1命名为活动但在〜/ .virtualenv / venv2中

cat activate* | grep "venv1"

shows 3 entries within the three files activate, activate.csh, and activate.fish

显示三个文件中的3个条目activate,activate.csh和activate.fish

In activate, change

在激活,更改

if [ "x(myvenv1) " != x ] ; then
        PS1="(myvenv1) $PS1"
else

to

...
        PS1="(myvenv2) $PS1"
...

In activate.csh change

在activate.csh中更改

if ("venv1" != "") then
        set env_name = "venv1"
else

to

...
    set env_name = "venv2"
...

In activate.fish change

在activate.fish改变

if test -n "(venv1) "
        printf "%s%s%s" "(venv1) " (set_color normal) (_old_fish_prompt)
        return
end

to

...
    printf "%s%s%s" "(venv2) " (set_color normal) (_old_fish_prompt)
...

Now when you source ~/.virtualenv/venv2/bin/activate or workon venv2 the command prompt will correctly display your environment (the cloned copy of venv1).

现在,当您使用〜/ .virtualenv / venv2 / bin / activate或workon venv2时,命令提示符将正确显示您的环境(venv1的克隆副本)。

Edit: this doesn't answer the question "How to copy modules from one virtualenv to another" but I'm pretty sure the result is in many cases the desired one, namely the creation of a new venv based on a previously created one which includes (all of) the previously installed modules.

编辑:这不回答“如何将模块从一个虚拟复制到另一个复制”的问题,但我很确定结果在很多情况下是所需的,即基于先前创建的一个新的venv的创建包括(所有)以前安装的模块。

#3


1  

Usually you are able to copy the .egg-info from the lib/site-packages folder of the virtualenv to the lib/site-packages of the other environment.

通常,您可以将.egg-info从virtualenv的lib / site-packages文件夹复制到其他环境的lib / site-packages。

#4


1  

seems like we can't just copy one virtualenv as another one. even you chnage the $VIRTUAL_ENV in the activate file, it still act as in origin virtualenv and pip will install all the packages to origin site-packages/

似乎我们不能只将一个虚拟复制品复制为另一个。即使你在激活文件中输入$ VIRTUAL_ENV,它仍然可以作为原始virtualenv,pip将所有包安装到origin site-packages /