简体   繁体   English

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

[英]how to copy modules from one virtualenv to another

是否可以将 python 模块从一个 virtualenv 复制到另一个 virtualenv。如果是这样,这是如何完成的?

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.但是,如果环境在不同的机器上或使用不同版本的 python 或其他一些主要差异,这可能不是一个好主意。 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.一般来说,生成一个requirements.txt更安全,然后使用它在其他环境中加载所有相同的模块。 You can create the file manually if you like, but it's easier to just use pip .如果愿意,您可以手动创建文件,但使用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

I am working on a 64bit machine with Ubuntu-14.04-64.我正在使用 Ubuntu-14.04-64 的 64 位机器上工作。 I compiled and installed python-3.4.3 to /opt/python3.4/ and created a vitualenv based on this python.我编译并安装了 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. PyQt5 就是这样工作的。 The command-line prompt still names venv1 as active but within ~/.virtualenv/venv2命令行提示符仍然将venv1 命名为活动的,但在~/.virtualenv/venv2 中

cat activate* | grep "venv1"

shows 3 entries within the three files activate , activate.csh , and activate.fish显示了三个文件activateactivate.cshactivate.fish 中的3 个条目

In activate, change在激活,改变

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

to

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

In activate.csh changeactivate.csh 中更改

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

to

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

In activate.fish changeactivate.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).现在,当您source ~/.virtualenv/venv2/bin/activateworkon 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.编辑:这并没有回答“如何将模块从一个 virtualenv 复制到另一个”的问题,但我很确定结果在很多情况下都是想要的,即基于先前创建的创建一个新的 venv包括(所有)以前安装的模块。

seems like we can't just copy one virtualenv as another one.似乎我们不能只是将一个 virtualenv 复制为另一个。 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,它仍然充当 origin virtualenv 并且 pip 会将所有软件包安装到 origin site-packages/

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

I was facing a problem installing 'wordcloud' on another Python venv on Windows 10. Package was installed on another project, same machine.我在 Windows 10 上的另一个 Python venv 上安装“wordcloud”时遇到问题。包安装在另一个项目,同一台机器上。

copy "wordcloud" and "wordcloud-1.8.1.dist-info" folders from Users/<user>/PycharmProjects/<projectname>/venv/Lib/site-packages and paste to your new project /<projectname>/venv/Lib/site-packages .Users/<user>/PycharmProjects/<projectname>/venv/Lib/site-packages复制“wordcloud”和“wordcloud-1.8.1.dist-info”文件夹并粘贴到您的新项目/<projectname>/venv/Lib/site-packages

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

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