Why Python Virtual Environments

The installation of Python has not been covered here as it has been covered in many tutorials out there in the web. However, for simplicity's sake, I will refer to two of my posts for Linux and Windows;

Virtual Environment

A virtual environment in Python is an isolated context for installing packages. It can also be referred to as a self-contained and light-weight Python installation. Virtual environments allow different projects to control or decide which versions of Python packages to use without interfering with other Python projects present in the installation host.

It is considered a best practice to always work inside a virtual environment as it avoids package version conflict with those that are installed globally, some of which could be dependencies of the operating system . With virtual environments, one does not have to install packages globally and this also has the advantage of not cluttering the default installation.

Users in the host machine can also create virtual environments without requiring administrative rights therefore making it easy to install packages. Also, different virtual environments can be based of different versions of Python as long as the version is already installed in the system e.g. system has Python3.6, 3.8 and 3.10 can have virtual environments created in those specific versions.

Components of Virtual Environment

Virtual environment consist of a directory that contains a copy of an existing Python installation plus s 'site-packages' directory in which packages can be installed that are specific to the virtual environment. Below is a list of directories and file contained in a virtual environment (Linux).

  • bin - folder containing activation scripts for the virtual environment and executable files as well.
  • include - this folder should house C header files if packages that depend on C are installed. It is initially empty on creation of the virtual environment.
  • lib - this folder contains the external packages that are installed for use within the virtual environment. The site-packages folder exists in this folder.
  • lib64 - This is a sybolic link to the lib folder listed above. It is important in Linux as it is used to distinguish between 32 and 64-bit architectures in libraries that are installed.
  • pyenv.cfg - This is a configuration file that determines which Python interpreter and site-packages directory the current Python session should use.

Creating a Virtual Environment

For Python version 3.3 and later, to create a virtual environment a module called venv is used as it should have been installed with Python itself. Run the following command in the command line:

python3 -m venv env

where env  is the name of the virtual environment that will be created (similar to one above).

Activating a Virtual Environment

After creating a virtual environment and prior to using it, it should be activated. To activate it, an activation script in the bin directory is used and this can be done using the following command:

source env/bin/activate

Notice the new name (env) in the prompt which indicates that we are in a virtual environment.

An alternative way of activating is by using a dot as shown below.

Deactivating a Virtual Environment

To deactivate a virtual environment, a deactivate command is used and it shall return us to the initial shell / parent shell from which the virtual environment was activated.

Conclusion

In future article(s) we will look at alternative packages that can be used in managing virtual environments for Python. For further reading, please refer to this article.

Comments

ABOUT ME
...

Hello and thanks for visiting! My name is Joseph Kariuki, and this is my website. I'm a software developer who seeks solutions in coding and writes tutorials and creates content on tech. I trust that you enjoy the post and have a great day.

DETAILS

Published: Sept. 23, 2022

TAGS
python fundamentals
CATEGORIES
Python Fundamentals