[英]How to rename a virtualenv in Python?
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
.我实际上打算创建名为
venv
的环境。 Having tried to rename the vnev
folder to venv
, I find that this doesn't provide much help.尝试将
vnev
文件夹重命名为venv
,我发现这并没有提供太多帮助。 The name of the activate environment still renames the old vnev
. activate 环境的名称仍然重命名旧的
vnev
。
$ mv vnev venv
$ . venv/bin/activate
(vnev) $ deactivate
I would like to know how to go about renaming the environment?我想知道如何重命名环境?
By default virtualenv does not support the renaming of environments.默认情况下,virtualenv 不支持环境重命名。 It is safer to just delete the virtualenv directory and create a new one with the correct name.
删除 virtualenv 目录并使用正确名称创建一个新目录更安全。 You can do this by:
您可以通过以下方式执行此操作:
source vnev/bin/activate
source vnev/bin/activate
pip freeze > requirements.txt
pip freeze > requirements.txt
rm -r vnev/
rm -r vnev/
virtualenv venv
virtualenv venv
source venv/bin/activate
source venv/bin/activate
pip install -r requirements.txt
pip install -r requirements.txt
If recreating is not an option there are 3rd party tools like virtualenv-mv that might be helpful.如果重新创建不是一种选择,那么像virtualenv-mv这样的 3rd 方工具可能会有所帮助。
Alternatively you can use virtualenvwrapper which provides the cpvirtualenv
command to copy or rename virtualenvs.或者,您可以使用virtualenvwrapper ,它提供
cpvirtualenv
命令来复制或重命名 virtualenv。
If you use virtualenvwrapper this 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 虚拟环境,请查看此问题。
cpvirtualenv
from virtualenv-wrapper errored out for me trying to run virtualenv-clone
, but running that directly worked fine:来自 virtualenv-wrapper 的
cpvirtualenv
在我尝试运行virtualenv-clone
出错,但直接运行它可以正常工作:
virtualenv-clone ~/.virtualenvs/oldname ~/.virtualenvs/newname
workon newname
rmvirtualenv oldname
No need to reinstall anything.无需重新安装任何东西。
The steps I use to rename a virtual environment:我用来重命名虚拟环境的步骤:
cp -a old_venv new_venv
sed
within the new_venv/bin
folder to directly change references to old_v.env
new_venv/bin
文件夹中使用sed
直接更改对old_v.env
引用cd new_venv/bin
sed -i 's/old_venv/new_venv/g' *
rm -rf old_env
Re-installing the ipykernel for jupyter may be required, but otherwise everything seems to work fine可能需要为 jupyter 重新安装 ipykernel,否则一切似乎都正常
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 将旧环境(例如 venv_1)克隆到新环境(例如 venv_2)。
conda create -n venv_2 --clone venv_1畅达创建 -n venv_2 --clone venv_1
This creates a new environment venv_2 cloning the venv_1.这将创建一个克隆 venv_1 的新环境 venv_2。 Hence no separate task of getting the packages/ dependencies.
因此没有单独的获取包/依赖项的任务。 Single step suffices.
一步就够了。
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 步即可完成任务。
In windows
I was able to easily rename my virtual environment by editing activate.bat
inside scripts\\
:在
windows
我可以通过在scripts\\
编辑activate.bat
来轻松重命名我的虚拟环境:
Backup the original activate.bat
(I copy&pasted then renamed mine BACKUP_activate.bat
).备份原始
activate.bat
(我复制并粘贴然后重命名我的BACKUP_activate.bat
)。
Right-click and edit activate.bat
.右键单击并编辑
activate.bat
。
Change VIRTUAL_ENV
variable from:将
VIRTUAL_ENV
变量从:
set VIRTUAL_ENV=C:\\some_dir\\old_venv_name
into进入
set VIRTUAL_ENV=C:\\some_dir\\new_venv_name
Change PROMPT
variable from:将
PROMPT
变量从:
set PROMPT=(old_venv_name) %PROMPT%
into进入
set PROMPT=(new_venv_name) %PROMPT%
Save the edited batch file保存编辑好的批处理文件
NOTE: My solution should work and save windows users
setting up new virtual environments, I have no knowledge scripting or whatsoever in linux or other operating systems注意:我的解决方案应该可以工作并保存
windows users
设置新的虚拟环境,我对 linux 或其他操作系统中的脚本或任何知识一无所知
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.