简体   繁体   English

如何在 Python 中重命名 virtualenv?

[英]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:您可以通过以下方式执行此操作:

  1. Activate your virtualenv: source vnev/bin/activate激活你的 virtualenv: source vnev/bin/activate
  2. Create a requirements.txt of currently installed packages: pip freeze > requirements.txt创建当前安装包的requirements.txt: pip freeze > requirements.txt
  3. Delete the misspelled virtualenv: rm -r vnev/删除拼写错误的 virtualenv: rm -r vnev/
  4. Create a new virtualenv with correct name: virtualenv venv使用正确的名称创建一个新的 virtualenv: virtualenv venv
  5. Activate new virtualenv: source venv/bin/activate激活新的 virtualenv: source venv/bin/activate
  6. Install packages from requirements.txt: pip install -r requirements.txt从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:我用来重命名虚拟环境的步骤:

  1. Copy the entire virtual environment folder to the new virtual environment.将整个虚拟环境文件夹复制到新的虚拟环境中。
cp -a old_venv new_venv
  1. Use sed within the new_venv/bin folder to directly change references to old_v.envnew_venv/bin文件夹中使用sed直接更改对old_v.env引用
cd new_venv/bin
sed -i 's/old_venv/new_venv/g' *
  1. Remove the old virtual environment删除旧的虚拟环境
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.我的答案类似于创建一个具有旧环境依赖项的新虚拟环境,但这个是简洁的。

  1. 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.一步就够了。

  1. 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来轻松重命名我的虚拟环境:

  1. Backup the original activate.bat (I copy&pasted then renamed mine BACKUP_activate.bat ).备份原始activate.bat (我复制并粘贴然后重命名我的BACKUP_activate.bat )。

  2. Right-click and edit activate.bat .右键单击并编辑activate.bat

  3. 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
  4. Change PROMPT variable from:PROMPT变量从:

     set PROMPT=(old_venv_name) %PROMPT%

    into进入

     set PROMPT=(new_venv_name) %PROMPT%
  5. 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.

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