133

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?

andrew
  • 4,991
  • 5
  • 24
  • 27
Kshitij Saraogi
  • 6,821
  • 8
  • 41
  • 71
  • Were you able to rename or recreate your virtualenv? – andrew Apr 18 '17 at 15:20
  • @andrew No. Atleast, not the way I wanted to. I wrote a script to install all the packages earlier installed in the wrong environment to the new environment. – Kshitij Saraogi Apr 18 '17 at 16:51
  • Does this answer your question? [Can I move a virtualenv?](https://stackoverflow.com/questions/32407365/can-i-move-a-virtualenv) – Mark Jun 06 '21 at 00:02

6 Answers6

223

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.

andrew
  • 4,991
  • 5
  • 24
  • 27
  • 3
    Is it necessary to deactivate the old virtualenv before deleting it? I didn't and it still worked but just curious. Thanks. – Bill Jul 25 '18 at 21:09
  • 2
    @Bill I believe its best practice but not required. Here is the deactivate script for virtualenv: https://github.com/pypa/virtualenv/blob/master/virtualenv_embedded/activate.sh#L4 – andrew Jul 30 '18 at 19:44
  • 1
    I liked the note on cpvirtualenv – RoyM Apr 12 '20 at 19:23
  • Does this go for a virtual environment created with pyenv as well? – Edison Dec 21 '20 at 04:26
  • So importantly this does not actual move any changes to the virtual environment along, right? Say I change a functon in Scipy it will just install the clean Scipy? (That would defeat the purpose of the virtual envirnoment for me.) – Kvothe Feb 25 '21 at 15:38
  • @Kvothe did you ever find a way to actually moving instead of recreating the virtual environment? – jdferreira Sep 30 '21 at 09:37
  • @jdferreira No I did not. – Kvothe Oct 01 '21 at 10:09
55

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.

farenorth
  • 10,165
  • 2
  • 39
  • 45
  • There seems to be a bug with cpvirutalenv, notably in ~///bin/pip and pip3.x is hardcoding paths. And copy done via cpvirtualenv is not updating those. Easy to fix, but without it standard operations like pip freeze or uinstall won't work – Drachenfels May 02 '18 at 12:18
14

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.env
cd new_venv/bin
# remove cache as sed would otherwise break with the `sed: couldn't edit __pycache__: not a regular file` error
rm -rf __pycache__/
sed -i 's/old_venv/new_venv/g' *
  1. Remove the old virtual environment
rm -rf old_venv

Re-installing the ipykernel for jupyter may be required, but otherwise everything seems to work fine

icedwater
  • 4,701
  • 3
  • 35
  • 50
Doster Esh
  • 141
  • 1
  • 4
3

In windows I was able to easily rename my virtual environment by editing activate.bat inside scripts\:

  1. Backup the original activate.bat (I copy&pasted then renamed mine BACKUP_activate.bat).

  2. Right-click and edit activate.bat.

  3. Change VIRTUAL_ENV variable from:

     set VIRTUAL_ENV=C:\some_dir\old_venv_name
    

    into

     set VIRTUAL_ENV=C:\some_dir\new_venv_name
    
  4. Change PROMPT variable from:

     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

  • 1
    There is a little snag with this. The Scripts directory contains a number of .exe files that are placed there when you've created the virtual environment and when installing packages using pip. They have the old path of the virtual environment hard-coded in them. This includes, for instance, pip.exe. You have to modify these files using a hex editor (or use sed on WSL or MINGW). – NZD Dec 04 '22 at 23:39
1

cpvirtualenv from virtualenv-wrapper errored out for me trying to run virtualenv-clone, but running that directly worked fine:

virtualenv-clone ~/.virtualenvs/oldname ~/.virtualenvs/newname
workon newname
rmvirtualenv oldname

No need to reinstall anything.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
0

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.

ImNomad
  • 51
  • 6