How To Set Up a Virtual Python Environment (Windows)¶
virtualenv is a tool to create isolated Python environments. You can read more about it in the
Virtualenv documentation. This article provides a quick summary to help
you set up and use a virtual environment.
Where’s My Python?¶
Sometimes the trickiest part of setting up a virtual environment on Windows is finding your python distribution. If
the installer didn’t add it to your
PATH variable, you may have to go looking. If you downloaded and installed
python from python.org and accepted all the defaults during installation,
be found in one of the following locations:
If you try to run
virtualenv and find it isn’t present, you can install it using
pip install virtualenv
virtualenv.exe will likely now be found in your python installation directory under the
Create a Virtual Python Environment¶
cd to your project directory and run
virtualenv to create the new virtual environment.
The following commands will create a new virtual environment under
cd my-project virtualenv --python C:\Path\To\Python\python.exe venv
Activate the Environment¶
Now that we have a virtual environment, we need to activate it.
After you activate the environment, your command prompt will be modified to reflect the change.
Add Libraries and Create a requirements.txt File¶
After you activate the virtual environment, you can add packages to it using
pip. You can also create a description
of your dependencies using
The following command creates a file called
requirements.txt that enumerates the installed packages.
pip freeze > requirements.txt
This file can then be used by collaborators to update virtual environments using the following command.
pip install -r requirements.txt
Deactivate the Environment¶
To return to normal system settings, use the
After you issue this command, you’ll notice that the command prompt returns to normal.