According to GeeksforGeeks, a virtual environment sometimes called venv or virtualenv is a tool that enables developers to keep dependencies required for different projects separate by creating an isolated environment for them. It makes it easy to install the exact version of python packages needed for a project without flooding the main python directory with unnecessary python packages.
USES OF VIRTUAL ENVIRONMENTS
Virtual environments have several uses which allow you to enjoy benefits like:
You can create a list of dependencies for your project which makes it easier for other developers to replicate your code base.
You can use any package for any python project of your choice without worrying about conflicts.
You can create isolated environments for your projects without crowding the main python directory with different unnecessary packages.
PREREQUISITES
Basic knowledge of python
Fundamentals of Command Line Interface Interaction
CREATING A VIRTUAL ENVIRONMENT
To create a virtual environment, you must have the virtualenv package installed in your base python directory. Open your terminal and run the command below:
pip install virtualenv
This command installs virtualenv.
In the terminal, create a new project directory, and name it ProjectVenv. Navigate into the ProjectVenv
directory by running cd ProjectVenv
in the terminal. After, type in the command below and click the enter key.
python -m venv env
The above command creates the virtual environment. The env
part of the command is the name of the virtual environment and can be called any name of your choice but it is a common habit among developers to name it env
ACTIVATING A VIRTUAL ENVIRONMENT
To be able to use a virtual environment, it needs to be activated. To activate your virtual environment, run the command particular to your operating system of the following:
ON A MAC OR LINUX MACHINE
source env/bin/activate
ON A WINDOWS MACHINE
source env/scripts/activate
You will immediately notice that your terminal path includes env
. This shows that you have activated and are currently in the virtual environment env
.
INSTALLING DEPENDENCIES IN A VIRTUAL ENVIRONMENT
Now that you have activated your virtual environment, let's install new libraries to see how virtual environments work. Run the command below in the terminal:
pip install python-decouple
The above command installs the python-decouple library which is used to separate settings or configurations from actual code. You can learn more about it here.
Run pip install flask
in the terminal. Flask is a lightweight python framework for building web applications.
It is important to note that your computer must have a stable internet connection for you to be able to install these libraries.
Now, run pip list
in the terminal. This command lists all the libraries you have installed in your virtual environment. The output in the terminal will look similar to the one below:
Package Version
------------------ -------
click 8.1.3
colorama 0.4.6
Flask 2.2.2
importlib-metadata 6.0.0
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.2
pip 22.0.4
python-decouple 3.7
setuptools 58.1.0
Werkzeug 2.2.2
zipp 3.12.0
The numbers which indicate the version of the libraries may differ, this is because libraries often get updated and the version changes. pip
installs the latest version of a library if you do not explicitly tell it to install a particular version. For example, if you run pip install flask-jwt-extended==4.4.4
in the terminal, you will be telling pip to install the version 4.4.4
of the flask-jwt-extended package. So, you use two equal operators ==
and the version number in that format to state the version of a package you wish to install.
You will also notice that there are more packages than we installed in the terminal. This happens because flask is a framework and it requires multiple libraries and packages to function so when you install flask, you install every package and library that it requires to work properly. You can check here for the differences between packages and libraries.
DEACTIVATING A VIRTUAL ENVIRONMENT
To deactivate your virtual environment, simply run deactivate
in the terminal. The env
in the terminal path disappears and the environment is deactivated.
VIRTUAL ENVIRONMENTS AND REQUIREMENT FILES
One of the very important uses of the virtual environment is in reducing dependency conflicts. We will be looking at how to go about that now.
Think of a situation where you are trying to import someone's code from a repository on github to your local machine. You will need to install all the required dependencies needed to run that code so you can run it successfully. Now, imagine this code base doesn't have a file that contains these dependencies and their respective versions, you will have to look through every import
statement in each file of the project to know the packages to install and as if that is not enough, even after knowing the packages to install you might install the wrong versions. Remember packages get updated often? and some of these packages may not be forward compatible, since pip
installs the latest version of the package, you start having issues with running the code.
This is where virtual environments and requirements file come into play. You already know what a virtual environment is but not a requirements file. A requirements file is a text file that contains a list of all of a project's dependencies. It is standard practice to have a requirements file for every project you create.
To create a requirement file for ProjectVenv
. Run the command below in the terminal:
pip freeze > requirements.txt
The above command generates a list of all the installed dependencies in the virtual environment env
and stores it in a requirements.txt
file. A new file requirements.txt
is now in the project directory, if you use an IDE you will already see it from the user interface. If you don't, you can run dir
in the terminal. dir
lists all the folders and files present in the project directory as output in the terminal, this output shows that there is a new file requirements.txt in the folder.
Open the file. You will see that the file contains the output from the pip list
command you ran earlier. Both commands are similar, the only difference is that pip list
shows the output in the terminal and pip freeze
saves it in a file.
You can call the file whatever you'd wish but it is conventional among developers to call it requirements.txt since the name is descriptive enough and also makes it easier to be identified.
It is standard to add a requirements file to your project's code base when uploading to web-based version control systems like GitHub in case someone needs to run your code on their local machine.
Now that you've learnt how to create a requirements file, it is necessary to know how to use a requirements file. Create a new file dependencies.txt
in the ProjectVenv
directory and copy the text below into the file:
alembic==1.9.2
aniso8601==9.0.1
attrs==22.2.0
click==8.1.3
colorama==0.4.6
Flask==2.2.2
Flask-JWT-Extended==4.4.4
Flask-Migrate==4.0.3
flask-restx==1.0.5
Flask-SQLAlchemy==3.0.2
Save the file and open the terminal. Run the command below, and make sure your virtual environment is activated.
pip install -r dependencies.txt
The command above installs all the listed packages present in the dependencies.txt
file. That is a simple method of installing all the dependencies needed for a project at a go rather than running the pip install
for each in the terminal.
CONCLUSION
In this tutorial, you have learnt how to create, activate and deactivate virtual environments. You also learnt how the concept of virtual environments and requirement files is used to reduce dependency conflicts. Virtual environments give you control over your python projects and ensure the main python environment is not congested with unnecessary python packages.