🚀 Join our community of 100,000+ learners worldwide! 📚 2000+ free tutorials available 🎓 Get certified today! 💡 Learn at your own pace 🌟 New courses added weekly 🤝 Connect with expert instructors
Python - Virtual Environment - Ngahtech Tutorials

Python - Virtual Environment

0 min read Python – Application Areas / Python – Application Areas
Free Text

Python - Virtual Environment

Python Virtual Environment

Python virtual environments create a virtual installation of Python inside a project directory. Users can then install and manage Python packages for each project. This allows users to install packages and modify their Python environment without fear of breaking packages installed in other environments.

What is Virtual Environment in Python?

A Python virtual environment is:

  • Considered as disposable.
  • Used to contain a specific Python interpreter and software libraries and binaries that are needed to support a project.
  • Contained in a directory, conventionally either named venv or .venv in the project directory.
  • Not considered as movable or copyable.

When you install Python software on your computer, it is available for use from anywhere in the filesystem. This is known as a system-wide installation.

While developing an application in Python, one or more libraries may be required to be installed using the pip utility (for example, pip3 install somelib). Moreover, an application (let us say App1) may require a particular version of a library, such as somelib 1.0. At the same time, another Python application (for example App2) may require a newer version of the same library, such as somelib 2.0. Installing the newer version may compromise the functionality of App1 because of conflicts between two different versions of the same library.

This conflict can be avoided by creating two isolated Python environments on the same machine. These isolated environments are called virtual environments. A virtual environment is a separate directory structure containing an isolated installation with a local copy of the Python interpreter, standard library, and other modules.

The purpose and advantage of using virtual environments is that multiple virtual environments can be created from a single global Python installation. Each virtual environment can contain a different version of the same library, thereby avoiding version conflicts between applications.

Creation of Virtual Environments in Python Using venv

This functionality is supported by the venv module included in the standard Python distribution.

Use the following commands to create a new virtual environment:

C:\Users\Acer>md pythonapp
C:\Users\Acer>cd pythonapp
C:\pythonapp>python -m venv myvenv

Here, myvenv is the folder in which a new Python virtual environment will be created, resulting in the following directory structure:

Directory of C:\pythonapp\myvenv

22-02-2023 09:53    <DIR>          .
22-02-2023 09:53    <DIR>          ..
22-02-2023 09:53    <DIR>          Include
22-02-2023 09:53    <DIR>          Lib
22-02-2023 09:53                 77 pyvenv.cfg
22-02-2023 09:53    <DIR>          Scripts

The utilities for activating and deactivating the virtual environment, as well as the local copy of the Python interpreter, are placed inside the Scripts folder.

Directory of C:\pythonapp\myvenv\Scripts

22-02-2023 09:53    <DIR>          .
22-02-2023 09:53    <DIR>          ..
22-02-2023 09:53              2,063 activate
22-02-2023 09:53                992 activate.bat
22-02-2023 09:53             19,611 Activate.ps1
22-02-2023 09:53                393 deactivate.bat
22-02-2023 09:53            106,349 pip.exe
22-02-2023 09:53            106,349 pip3.10.exe
22-02-2023 09:53            106,349 pip3.exe
22-02-2023 09:53            242,408 python.exe
22-02-2023 09:53            232,688 pythonw.exe

Activating Virtual Environment

To enable the newly created virtual environment, execute activate.bat from the Scripts folder.

C:\pythonapp>myvenv\scripts\activate

(myvenv) C:\pythonapp>

Notice the name of the virtual environment displayed inside parentheses. The Scripts folder contains a local copy of the Python interpreter, allowing you to start a Python session within this virtual environment.

Checking If Python is Running Inside a Virtual Environment

To confirm whether the current Python session is running inside a virtual environment, check the value of sys.path.

(myvenv) C:\pythonapp>python

Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37)
[MSC v.1929 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys
>>> sys.path

['',
 'C:\\Python310\\python310.zip',
 'C:\\Python310\\DLLs',
 'C:\\Python310\\lib',
 'C:\\Python310',
 'C:\\pythonapp\\myvenv',
 'C:\\pythonapp\\myvenv\\lib\\site-packages']

>>>

The Scripts folder of the virtual environment also contains the pip utilities. Any package installed from PyPI using pip will be available only within the currently active virtual environment.

Deactivating Virtual Environment

To deactivate the virtual environment and return to the system-wide Python installation, run the deactivate.bat script.

(myvenv) C:\pythonapp>deactivate

After deactivation, the command prompt will return to its normal state, and the global Python interpreter will be used again.