如何在 Python 中重命名 virtualenv? [英] How to rename a virtualenv in Python?
问题描述
我在初始化时拼错了 virtualenv
的名称:
I misspelled the name of the virtualenv
while initializing it using:
$ virtualenv vnev
我实际上打算创建名为 venv
的环境.尝试将 vnev
文件夹重命名为 venv
后,我发现这并没有提供太多帮助.激活环境的名称仍然重命名旧的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
.
$ mv vnev venv
$ . venv/bin/activate
(vnev) $ deactivate
我想知道如何重命名环境?
I would like to know how to go about renaming the environment?
推荐答案
默认情况下,virtualenv 不支持环境重命名.删除 virtualenv 目录并使用正确名称创建一个新目录更安全.您可以通过以下方式执行此操作:
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:
- 激活你的虚拟环境:
source vnev/bin/activate
- 为当前安装的包创建一个requirements.txt:
pip freeze >要求.txt
- 删除拼写错误的 virtualenv:
rm -r vnev/
- 使用正确的名称创建一个新的 virtualenv:
virtualenv venv
- 激活新的virtualenv:
source venv/bin/activate
- 从requirements.txt安装包:
pip install -r requirements.txt
如果重新创建不是一种选择,那么可能有像 virtualenv-mv 这样的 3rd 方工具有帮助.
If recreating is not an option there are 3rd party tools like virtualenv-mv that might be helpful.
或者,您可以使用 virtualenvwrapper,它提供了cpvirtualenv
命令复制或重命名 virtualenvs.
Alternatively you can use virtualenvwrapper which provides the cpvirtualenv
command to copy or rename virtualenvs.
这篇关于如何在 Python 中重命名 virtualenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!