How to rename a virtualenv in Python?

孤街浪徒 提交于 2019-12-18 10:11:33

问题


I misspelled the name of the virtualenv while initializing it using:

$ 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.

$ mv vnev venv
$ . venv/bin/activate
(vnev) $ deactivate

I would like to know how to go about renaming the environment?


回答1:


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:

  1. Activate your virtualenv: source vnev/bin/activate
  2. Create a requirements.txt of currently installed packages: pip freeze > requirements.txt
  3. Delete the misspelled virtualenv: rm -r vnev/
  4. Create a new virtualenv with correct name: virtualenv venv
  5. Activate new virtualenv: source venv/bin/activate
  6. Install packages from 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.

Alternatively you can use virtualenvwrapper which provides the cpvirtualenv command to copy or rename virtualenvs.




回答2:


If you use virtualenvwrapper this can be done by:

$ cpvirtualenv <wrong_name> <correct_name>
$ rmvirtualenv <wrong_name>

Also, FYI, to rename a conda virtualenvironment, check out this question.




回答3:


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 create -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.

  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"

So in 1/2 steps the task can be achieved.



来源:https://stackoverflow.com/questions/43256369/how-to-rename-a-virtualenv-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!