This question is not a duplicate.
It pertains not just to renaming a virtual environment, but to actually moving it to a different directory, incl
venv
built-in module)As of Python v3.3, virtualenv
has become a built-in module named venv
.
The --relocatable
option mentioned in other answers has not been included in venv
, and currently there is no good, safe way that I'm aware of to either rename or relocate a Python virtual environment.
However, there is a fairly simple way to simply recreate a virtual environment, with all its current installed packages. See this answer, or see the section below for information on recreating a virtual environment. During the process you can recreate the new environment in whatever location and with whatever name you desire. Or see the section below for the process.
In that answer, he does mention a few other 3rd party packages which may support direct renames or moves. If you are settled on pursuing a way to move a virtual environment intact, you could look into if those work with venv
as well.
Note: In that answer, it is focused on virtualenv
, rather than venv
. See below for how to translate.
venv
vs. older virtualenv
command syntaxThe command to use venv
is:
python -m venv
rather than just virtualenv
, which installs as a command in the original package. Where "python" refers to however you run your python executable, which could be a variety of things, such as:
python
py
or py -3.7
or similar (the Python Launcher for Windows for Python 3.3+ and Windows only at the moment)python3
(convention for linux environments that dual install python 2 and 3)c:\program files\python37\python.exe
If you are unsure which version is being run, you can always python --version
to find out.
Creating/recreating a virtual environment is easy and should become second nature after you work with them for a bit. This process mirrors what you would do to distribute your script as a package (with it's dependencies) in the first half, and then what someone would do to install your script/package for further development.
First, get an updated list of what is in the virtual environment. With it active, get the Python version it uses and save out the list of dependencies to a file.
Use python --version
with the virtual environment activated to see what version of Python it is using.
Use python -m pip freeze > requirements.txt
to create the list of current package dependencies and put them into the requirements.txt
file. This command works in Linux or the Git Bash for sure - not 100% sure about Powershell or Command Line in Windows.
Now create a new virtual environment and then add the dependencies from the old one.
Make your new venv.
python
as the commandpython.exe
in the virtual environment folderpython -m venv my_new_venv
to create a new virtual environment in the current working directory in a new my_new_venv
folder.Install your dependencies from the requirements.txt
file.
python -m pip install -r requirements.txt
You might need to reinstall local packages that are in development mode.
Note, if you ever need to see the specific location a package is installed to, use:
python -m pip list -v
-v
or "verbose" option will add some extra information about each package that is installed, including the path it is installed in. This is useful to make sure you are keeping virtual, user, and system installed packages straight.At this point you can just delete the old venv folder and all contents. I recommend using a GUI for that - file deletions are often permanent from the linux command line, and a small typo can be bad news.