如何在 Python 中重命名 virtualenv?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43256369/
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
How to rename a virtualenv in Python?
提问by Kshitij Saraogi
I misspelled the name of the virtualenv
while initializing it using:
我在virtualenv
使用以下方法初始化它时拼错了名称:
$ virtualenv vnev
I actually intended to create the environment with the name venv
.
Having tried to rename the vnev
folder to venv
, I find that this doesn't provide much help. The name of the activate environment still renames the old vnev
.
我实际上打算用名称创建环境venv
。尝试将vnev
文件夹重命名为 后venv
,我发现这并没有提供太多帮助。激活环境的名称仍然重命名旧的vnev
.
$ mv vnev venv
$ . venv/bin/activate
(vnev) $ deactivate
I would like to know how to go about renaming the environment?
我想知道如何重命名环境?
回答by andrew
By default virtualenv does not support the renaming of environments. It is safer to just delete the virtualenv directory and create a new one with the correct name. You can do this by:
默认情况下,virtualenv 不支持环境的重命名。删除 virtualenv 目录并使用正确名称创建一个新目录更安全。您可以通过以下方式执行此操作:
- Activate your virtualenv:
source vnev/bin/activate
- Create a requirements.txt of currently installed packages:
pip freeze > requirements.txt
- Delete the misspelled virtualenv:
rm -r vnev/
- Create a new virtualenv with correct name:
virtualenv venv
- Activate new virtualenv:
source venv/bin/activate
- Install packages from requirements.txt:
pip install -r requirements.txt
- 激活您的虚拟环境:
source vnev/bin/activate
- 创建当前安装的包的requirements.txt:
pip freeze > requirements.txt
- 删除拼写错误的 virtualenv:
rm -r vnev/
- 创建一个具有正确名称的新 virtualenv:
virtualenv venv
- 激活新的 virtualenv:
source venv/bin/activate
- 从 requirements.txt 安装包:
pip install -r requirements.txt
If recreating is not an option there are 3rd party tools like virtualenv-mvthat might be helpful.
如果重新创建不是一种选择,那么像virtualenv-mv这样的3rd 方工具可能会有所帮助。
Alternatively you can use virtualenvwrapperwhich provides the cpvirtualenv
command to copy or rename virtualenvs.
或者,您可以使用virtualenvwrapper,它提供cpvirtualenv
复制或重命名 virtualenvs的命令。
回答by farenorth
If you use virtualenvwrapperthis can be done by:
如果您使用virtualenvwrapper,则可以通过以下方式完成:
$ cpvirtualenv <wrong_name> <correct_name>
$ rmvirtualenv <wrong_name>
Also, FYI, to rename a conda virtualenvironment, check out this question.
另外,仅供参考,要重命名 conda 虚拟环境,请查看此问题。
回答by ImNomad
My answer is similar to creating a new virtual environment with the dependencies of the old one, but this one is succinct.
我的答案类似于创建一个具有旧环境依赖项的新虚拟环境,但这个是简洁的。
Clone the old environment (say venv_1) to a new environment (say venv_2) using conda.
conda create -n venv_2 --clone venv_1
使用 conda 将旧环境(例如 venv_1)克隆到新环境(例如 venv_2)。
畅达创建 -n venv_2 --clone venv_1
This creates a new environment venv_2 cloning the venv_1. Hence no separate task of getting the packages/ dependencies. Single step suffices.
这将创建一个克隆 venv_1 的新环境 venv_2。因此没有单独的获取包/依赖项的任务。一步就够了。
Delete the old virtual environment. [This step is optional if you still want to keep the old environment]
rm -rf "fully qualified path of the old virtual environment"
删除旧的虚拟环境。【如果你还想保留旧环境,这一步是可选的】
rm -rf "旧虚拟环境的完全限定路径"
So in 1/2 steps the task can be achieved.
因此,只需 1/2 步即可完成任务。