For Python 3.3+ (with new venv
built-in module)
Short Answer (regardless of version):
- There's no clean, direct way to move a virtual environment
- Just recreate, it's easy!!
Long Answer:
As of Python v3.3, the virtualenv
package 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, it is fairly simple to recreate a virtual environment, with all its currently installed packages. See this answer, or see the section below. During the process you can recreate the new environment in whatever location and with whatever name you desire.
In the answer linked above, he mentions some 3rd party packages which may support direct renames or moves. If you are settled on pursuing a way to move a virtual environment, you could look into if those work with venv
as well.
Note: In that answer, it is focused on virtualenv
, rather than venv
. See next section for how to translate.
Note 2: There are some special cases where it may be more difficult to recreate the virtual environment - such as being offline or having a particularly large set of dependencies that have to build, etc. I don't cover those exceptions here, but see the comments for some ideas.
venv
vs. older virtualenv
command syntax
The 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 bundled with Python for Windows, or the py
package that can be installed separately for Linux [and MacOS?])
python3
(convention for linux environments that dual install python 2 and 3)
- If you are having issues, use the absolute path to the python executable you want to run: e.g.
c:\program files\python37\python.exe
If you are unsure which version is being run, you can always python --version
to find out.
How to recreate a virtual environment
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.
- This is for clarity - you may want to update the Python version for various reasons - at least to the latest patch version
- For example, if the existing venv is using Python v3.7.4, but now v3.7.6 is out - use v3.7.6 instead, which should including only non-breaking security and bug fixes.
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.
- Make sure you are using the correct version of python that you want to install to the venv.
- If you want it to be exactly the same Python version:
- While in the old venv, type "python --version", then make sure you create the new venv with that version of the python command.
- For the new venv folder entry in the command:
- Either add an absolute or relative path to the desired final folder location.
- Use
python -m venv my_new_venv
to create a new virtual environment in the current working directory in a new my_new_venv
folder.
- The name of the venv folder will be the name of the venv (what shows up in the prompt when it is activated).
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
- The
-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.
virtualenv ./my-python-venv
) - suggesting it may be "portable". Restricting this destination to be an absolute-path would reinforce the notion that thevirtualenv
directory cannot-be/should-not be moved.mv
command.