Python 我可以移动一个 virtualenv 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32407365/
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-19 11:30:31  来源:igfitidea点击:

Can I move a virtualenv?

pythonpython-3.xvirtualenvvirtualenv-commands

提问by Nathan Basanese

This question is not a duplicate.

这个问题不是重复的。

It pertains not just to renaminga virtual environment, but to actually movingit to a different directory, including, potentially, a different user's directory.

它不仅涉及重命名虚拟环境,还涉及实际移动到不同的目录,可能包括不同的用户目录。

This is not the same as merely renaming a virtual environment, especially to people unfamiliar with virtualenvs.

这与仅仅重命名虚拟环境不同,特别是对于不熟悉 virtualenvs 的人。

If I create a virtualenv, and I move it to a different folder, will it still work?

如果我创建了一个 virtualenv,并将它移动到另一个文件夹,它还能工作吗?

$ virtualenv -p /usr/bin/python3 /home/me/Env/my-python-venv
$ source Env/my-python-venv/bin/activate
(my-python-venv) $ 

...later that day, the virtual environment MOVED...

...那天晚些时候,虚拟环境移动了...

(my-python-venv) $ deactivate
$ mkdir -p /home/me/PeskyPartyPEnvs
$ mv /home/me/Env/my-python-venv /home/me/PeskyPartyPEnvs/

Question:

题:

Will this work?

这会起作用吗?

$ source /home/me/PeskyPartyPEnvs/my-python-venv/bin/activate
(my-python-venv) $ /home/me/PeskyPartyPEnvs/my-python-venv/bin/pip3 install foaas

I mean this as less of a question about the wisdom of trying this (unless that wisdom is humorous, of course), and more about whether it's possible. I really want to know whether it's possible to do in Python 3, or whether I just have to suck it upand clone it.

我的意思是这不是一个关于尝试这个是否明智的问题(当然,除非这种智慧是幽默的),更多的是关于它是否可能。我真的很想知道是否可以在 Python 3 中做到这一点,或者我是否只需要吸收它并克隆它。

Can I just mva virtualenvlike that without sadness? I do want to avoid sadness.

可我只是mv一个virtualenv喜欢,没有悲伤?我确实想避免悲伤。

采纳答案by Nathan Basanese

BUT ALAS:

可惜:

No, you can't simply mv. There are workarounds, but it might be easier to reinstall.

不,你不能简单地mv。有解决方法,但重新安装可能更容易。

(my-python-venv)$ /home/me/PeskyPartyPEnvs/pip3 install foaas
zsh: /home/me/PeskyPartyPEnvs/pip3: bad interpreter: /home/me/Env/my-python-venv/bin/python3: no such file or directory
(my-python-venv)$ deactivate
$ 

... presses entera lot in frustration, and the following works

...enter沮丧地压了很多,下面的工作

$
$
$ pip3 search foaas

Except it is not from my-python-venv, ergo sadness.

除了它不是来自my-python-venv,所以悲伤。

Want to mvyour virtualenvand use it, otherwise unmodified?

想要mv你的virtualenv和使用它,否则未经修改?

Short Answer:

简答:

I'll let Boromir say it, so he can make it clear:

我让博罗米尔说出来,让他说清楚:

Well, ya can't.

嗯,你不能

回答by Simeon Visser

Yes, this should be possible if you haven't done anything that depends on the current directory of the virtualenv.

是的,如果你没有做任何依赖于 virtualenv 的当前目录的事情,这应该是可能的。

However, if you have the choice, the best thing to do is to create new virtualenv and start using the new virtualenv instead. This is the safest choice and least likely to cause issues later.

但是,如果您有选择,最好的办法是创建新的 virtualenv 并开始使用新的 virtualenv。这是最安全的选择,以后最不可能引起问题。

The documentation does mention that:

文档确实提到

Each virtualenv has path information hard-coded into it,

每个 virtualenv 都有硬编码的路径信息,

For example, if you have run setvirtualenvprojectthen it won't be able to switch to the right directory after you run workon ...so in that case you'd need to fix that manually.

例如,如果您已经运行,setvirtualenvproject那么它在您运行后将无法切换到正确的目录,workon ...因此在这种情况下您需要手动修复该问题。

In general a virtualenv is little more than a directory with the necessary Python interpreter files plus packages that you need.

通常,virtualenv 只不过是一个包含必要的 Python 解释器文件和您需要的包的目录。

回答by Ruth Franklin

The --relocatableargument to virtualenvappears to allow you to do this.

--relocatableto的参数virtualenv似乎允许您这样做。

回答by hilcharge

Yes. It is possible to move it on the same platform. You can use --relocatableon an existing environment.

是的。可以在同一平台上移动它。您可以--relocatable在现有环境中使用。

From --help:

来自--help

--relocatable -- Make an EXISTING virtualenv environment relocatable. This fixes up scripts and makes all .pth files relative.

--relocatable -- 使现有的 virtualenv 环境可重定位。这修复了脚本并使所有 .pth 文件相对。

HOWEVER, this does NOT seem to change the activatescript, and rather only changes the pip*and easy_install*scripts. In the activatescript, the $VIRTUAL_ENVenvironment variable hardcoded as the original /path/to/original/venv. The $VIRTUAL_ENVvariable is used to set the PATHof your active environment too, so it must be changed based on the new location in order to call pythonand pipetc. without absolute path.

但是,这似乎不会更改activate脚本,而只会更改pip*easy_install*脚本。在activate脚本中, $VIRTUAL_ENV环境变量硬编码为原始/path/to/original/venv. 该$VIRTUAL_ENV变量用于设置PATH你的活动环境中也是如此,所以它必须根据新的位置,以调用改变pythonpip等没有绝对路径。

To fix this issue, you can change the $VIRTUAL_ENVenvironment variable in the activatescript (for example using sed), and everything should be good to go.

要解决此问题,您可以更改脚本中的$VIRTUAL_ENV环境变量activate(例如使用sed),一切都会顺利进行。

An example of usage:

用法示例:

$ cd ~/first
$ virtualenv my-venv
$ grep 'VIRTUAL_ENV=' my-venv/bin/activate
VIRTUAL_ENV="/home/username/first/my-venv"
$ virtualenv --relocatable my-venv
Making script my-venv/bin/easy_install relative
Making script my-venv/bin/easy_install-2.7 relative
Making script my-venv/bin/pip relative
Making script my-venv/bin/pip2 relative
Making script my-venv/bin/pip2.7 relative
### Note that `activate` has not been touched
$ mkdir ~/second
$ mv my-venv ~/second
$ cd ~/second
$ grep 'VIRTUAL_ENV=' my-venv/bin/activate
VIRTUAL_ENV=/home/username/first/my-venv
### (This variable hasn't been changed, it still refers to the old, now non-existent directory!)
$ sed -i -e 's|username/first|username/second|' my-venv/bin/activate
## sed can be used to change the path.
## Note that the `-i` (in place) flag won't work on all machines. 
$ source my-venv/bin/activate 
(my-venv) $ pip install foass
...
(my-venv) $ python 
[...]
> import foass

Hooray, now you can install things and load them into your newly located virtual environment.

万岁,现在你可以安装东西并将它们加载到你新定位的虚拟环境中。

回答by Gerbs

Using answers of this and other threads about similar topic, I've made a bash script that, located and executed within the virtualenv directory itself, will help with your virtualenv moves.

使用该主题和其他主题的类似主题的答案,我制作了一个 bash 脚本,该脚本位于virtualenv 目录本身中在其中执行,将有助于您的 virtualenv 移动。

After doing virtualenv --relocatable yourenvyou'll need to change your VIRTUAL_ENVvariable every time you move the directory, so if you don't wan't to change it manually, use this.

完成后,每次移动目录时virtualenv --relocatable yourenv都需要更改VIRTUAL_ENV变量,因此如果您不想手动更改它,请使用它。

#!/bin/bash \n 
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
EXISTING=$(grep 'VIRTUAL_ENV=' bin/activate)  
NEWDIR=VIRTUAL_ENV=\"$DIR\"
sed -i -e "s|$EXISTING|$NEWDIR|" bin/activate
source bin/activate

I hope it helps.

我希望它有帮助。

回答by LightCC

For Python 3.3+ (with new venvbuilt-in module)

对于 Python 3.3+(带有新的venv内置模块)

Short Answer (regardless of version):

简答(不分版本):

  • Just recreate, it's easy!!
  • It's difficult to move/rename virtual environments...
  • 只需重新创建,这很容易!!
  • 移动/重命名虚拟环境很困难......


Long Answer:

长答案:

As of Python v3.3, virtualenvhas become a built-in module named venv.

从 Python v3.3 开始,virtualenv已经成为一个名为venv.

The --relocatableoption mentioned in other answers has not been included in venv, and currently there is no good, safe way that I'm aware of to either rename or relocate a Python virtual environment.

--relocatable其他答案中提到的选项没有包含在 中venv,目前我知道没有好的、安全的方法来重命名或重新定位 Python 虚拟环境。

However, there is a fairly simple way to simply recreate a virtual environment, with all its current installed packages. See this answerabout renaming a virtual environment. During the process you can recreate the new environment in whatever location and with whatever name you desire.

然而,有一种相当简单的方法可以简单地重新创建一个虚拟环境,以及它当前安装的所有包。请参阅有关重命名虚拟环境的答案。在此过程中,您可以在任何位置以您想要的任何名称重新创建新环境。

In that answer, he does mention a few other 3rd party packages which may support direct renames or moves. If you are settled on pursuing a way to move a virtual environment intact, you could look into if those work with venvas well.

在那个答案中,他确实提到了其他一些可能支持直接重命名或移动的 3rd 方软件包。如果您决定寻求一种完整地移动虚拟环境的方法,您可以研究这些方法是否也venv适用。

Note:In that answer, it is focused on virtualenv, rather than venv. See the last section for how to translate.

注意:在那个答案中,它侧重于virtualenv,而不是venv。有关如何翻译,请参阅最后一节。



The basic process to recreate a virtual environment

重新创建虚拟环境的基本过程

Creating/recreating a virtual environment is easy and should become second nature after you work with them for a bit. This mirrors what you would do to distribute your script with it's dependencies in version control in the first half, then what someone installing your script or package would do to setup the script to develop it further.

创建/重新创建虚拟环境很容易,并且在您使用它们一段时间后应该成为您的第二天性。这反映了您在前半部分使用版本控制中的依赖项分发脚本时会做的事情,然后安装您的脚本或包的人会做些什么来设置脚本以进一步开发它。

First, get an updated list of what is in the virtual environment. With it active, get the Python version it uses and save out the list of dependencies to a file.

首先,获取虚拟环境中内容的更新列表。激活它后,获取它使用的 Python 版本并将依赖项列表保存到文件中。

  1. Use python --versionwith the virtual environment activated to see what version of Python it is using.

    • This is for clarity - you may want to update the Python version for various reasons - at least to the latest patch version
    • For example, if the existing venv is using Python v3.7.4, but now v3.7.6 is out - use v3.7.6 instead, which should including only non-breaking security and bug fixes.
  2. Use python -m pip freeze > requirements.txtto create the list of current package dependencies and put them into the requirements.txtfile. This command works in Linux or the Git Bash for sure - not 100% sure about Powershell or Command Line in Windows.

  1. python --version与激活的虚拟环境一起使用以查看它使用的 Python 版本。

    • 这是为了清楚起见 - 您可能出于各种原因想要更新 Python 版本 - 至少更新到最新的补丁版本
    • 例如,如果现有的 venv 正在使用 Python v3.7.4,但现在 v3.7.6 已过时 - 请改用 v3.7.6,它应仅包括非破坏性的安全性和错误修复。
  2. 使用python -m pip freeze > requirements.txt创建当前软件包的依赖关系的列表,并把它们放进requirements.txt文件。这个命令肯定适用于 Linux 或 Git Bash - 不是 100% 确定 Windows 中的 Powershell 或命令行。

Now create a new virtual environment and then add the dependencies from the old one.

现在创建一个新的虚拟环境,然后添加旧环境的依赖项。

  1. Make your new venv.

    • Make sure you are running the correct version of python that you want to install to the venv.
    • If you want it to be exactly the same Python version:
      • Run python directly from the current virtual environment (with it activated), and just use pythonas the command
      • Or use an absolute path to run python.exein the virtual environment folder
    • For the new venv folder entry in the command:
      • Either add an absolute or relative path to the desired final folder location.
      • Use python -m venv my_new_venvto create a new virtual environment in the current working directory in a new my_new_venvfolder.
      • The name of the venv folder will be the name of the venv (what shows up in the prompt when it is activated).
  2. Install your dependencies from the requirements.txtfile.

    • python -m pip install -r requirements.txt.
  1. 制作您的新 venv。

    • 确保您正在运行要安装到 venv 的正确版本的 python。
    • 如果您希望它是完全相同的 Python 版本:
      • 直接从当前虚拟环境运行python(激活它),并直接python用作命令
      • 或者使用绝对路径python.exe在虚拟环境文件夹中运行
    • 对于命令中的新 venv 文件夹条目:
      • 添加到所需最终文件夹位置的绝对或相对路径。
      • 用于python -m venv my_new_venv在新my_new_venv文件夹的当前工作目录中创建新的虚拟环境。
      • venv 文件夹的名称将是 venv 的名称(激活时在提示中显示的内容)。
  2. requirements.txt文件安装您的依赖项。

    • python -m pip install -r requirements.txt.

You might need to reinstall local packages that are in development mode. Note, if you ever need to see the specific location a package is installed to, use python -m pip list -v. The -vor "verbose" option will add some extra information about each package that is installed, including the path it is installed in. This is useful to make sure you are keeping virtual, user, and system installed packages straight.

您可能需要重新安装处于开发模式的本地包。请注意,如果您需要查看安装包的特定位置,请使用python -m pip list -v. 的-v或“详细”选项将增加大约每个安装的软件包,包括它安装在路径一些额外的信息,这是非常有用的,以确保你保持虚拟,用户和系统安装的软件包直。

At this point you can just delete the old venv folder and all contents. I recommend using a GUI for that - file deletions can be pretty permanent from the linux command line, and a small typo could be bad news.

此时您可以删除旧的 venv 文件夹和所有内容。我建议为此使用 GUI - 从 linux 命令行删除文件可能是永久性的,一个小的错字可能是个坏消息。



venvvs. older virtualenvcommand syntax

venv与较旧的virtualenv命令语法

The command to use venvis:

要使用的命令venv是:

python -m venv

rather than just virtualenv, which installs as a command in the original package. Where "python" refers to however you run your python executable, which could be a variety of things, such as:

而不仅仅是virtualenv,它作为命令安装在原始包中。其中“python”是指您运行 python 可执行文件的方式,这可能是多种情况,例如:

  1. python
  2. pyor py -3.7or similar (the Python Launcher for Windowsfor Python 3.3+ and Windows only at the moment)
  3. python3(convention for linux environments that dual install python 2 and 3)
  4. If you are having issues, use the absolute path to the python executable you want to run: e.g. c:\program files\python37\python.exe
  1. python
  2. pypy -3.7或类似的(目前仅适用于 Python 3.3+ 和 Windows的Python Launcher for Windows
  3. python3(双重安装python 2和3的linux环境的约定)
  4. 如果遇到问题,请使用要运行的 python 可执行文件的绝对路径:例如 c:\program files\python37\python.exe

If you are unsure which version is being run, you can always python --versionto find out.

如果您不确定正在运行哪个版本,您可以随时python --version了解。