Steps used to create a virtual environment::
- Make sure you’ve got Python & pip.
python --version
python -m pip --version
If pip is not installed or not up-to-date, you can install/upgrade using
python3 -m pip install --user --upgrade pip
-
Install virtualenv via pip:
python3 -m pip install --user virtualenv
Test your installation:python3 -m virtualenv --version
-
Create a virtual environment for a project:
cd my_project_folder
python3 -m venv env
** It's a good idea to create the environment inside your project folder. You can name it anything (venv is standard)
You can also use the Python interpreter of your choice (like python2.7).
virtualenv -p /usr/bin/python2.7 venv
-
To begin using the virtual environment, it needs to be activated:
source venv/bin/activate
-
If you are done working in the virtual environment for the moment, you can deactivate it:
deactivate
This puts you back to the system’s default Python interpreter with all its installed libraries. -
To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf my_project.)
-
In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run:
pip freeze > requirements.txt
-
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using pip list. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:
pip install -r requirements.txt
** This can help ensure consistency across installations, across deployments, and across developers. -
Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.