A Python virtual environment (venv) supports for managing your dependencies without affecting with default environment. This venv is based in your own site directories. The venv is a copy of an existing version Python. It is really useful when you develop a project. Helping you to manage your own site-packages, avoiding the changes which might cause nightmares, between the current version of the library (working on your project) and the latest version of the library in the future.
It’s too bad every package is installed in the default system of Python. The venv is part of the most essential work most Python developers use.
An installation of Python
Opening the terminal and changing direction to your project.
There are many ways to create a virtual environment, which include using virtualenv or venv or conda. If you’re using Python 2, you have to install the virtualenv with pip or use conda. In this article, I just mention to create venv via virtualenv and venv.
For Python 2, you have to install virtualenv to create vitural environment. Python 3 can also use this way.
In the terminal of Windows (I use powershell):
In the terminal of MacOS/ Linux:
For Python 3:
For Python 2:
Next, type the following:
where .envName
is the name of your virtual environment (you can change it).
Since version 3.3, Python 3 has come with a built-in venv
module. To use the module, in your terminal:
python3 -m venv .envName
where .envName
is the name of your virtual environment (you can change it).
Type ‘python’ instead of ‘python3’ if you use Windows.
Activate the environment by running the following command:
MacOS/Linux:
Windows:
The name of your venv will appear on the left side of your terminal when it is active ((.envName)
in this case)
To deactivate the venv, type deactivate
deactivate
In order to automatically save all packages to the text file that have been installed in the venv.
For Python 2 or Windows use below:
pip freeze > requirements.txt
For Python 3:
pip3 freeze > requirements.txt
In order to install packages from requirements.txt
:
pip install -r requirements.txt
You should also add the venv to .gitignore
file to exclude this folder if you use Git.
That’s all being needed to start a project with Python Virtual Environment. Lastly, I highly recommend creating a venv before starting any project, which will help you become more professional.