Virtual Environment

Python’s Virtual Environment is a self-contained directory tree that includes a Python installation and number of additional packages. It provides an isolated environment where you can install specific versions of libraries and dependencies without affecting the global Python environment.

Why Utilize a Virtual Environment?

Working with Python often involves using different libraries or modules. Different projects might require different versions of these libraries. To avoid version conflicts among projects, it’s a best practice to use a separate virtual environment for each Python project.

This enables you to work on various Python projects with different library versions on the same machine without one project’s dependencies interfering with another’s.

Creating a Virtual Environment

Below we will show you how to create a virtual environment in different operation systems.

Activating the Virtual Environment

After creating the virtual environment, you need to activate it before using.

After activation, your shell’s prompt will change to show the name of the active virtual environment.

Remember to deactivate the virtual environment when you’re done: just type deactivate in your terminal.

Note

Ensure that you have the appropriate Python version installed and the venv module is available. The venv module was added in Python 3.4. For older Python versions, you may use the virtualenv tool instead.

With virtual environments, you can keep the dependencies required by different projects in separate places, and manage them effectively. This helps maintain clean, professional, and manageable coding environments.