Python Virtual Environments 101: Understanding the Basics

Amit Randive
2 min readJan 14, 2023

--

The Key to Managing Dependencies and Isolating Projects

Photo by Lucrezia Carnelos on Unsplash

Python virtual environments are an essential tool for managing dependencies and isolating different projects on a single machine. A virtual environment is a separate Python installation that allows you to install packages and run Python scripts without interfering with the system-wide Python installation.

Creating a virtual environment is simple and can be done using the built-in venv module in Python 3 or the virtualenv package for Python 2. To create a virtual environment, simply run the following command in your terminal:

python3 -m venv myenv

This will create a new directory called “myenv” in your current working directory. This directory contains the Python executable and all the necessary libraries and modules for your virtual environment.

To activate your virtual environment, simply run the activate script in the environment’s bin directory:

source myenv/bin/activate

You can then install packages and run Python scripts as you would normally, but any packages you install will be isolated to this specific virtual environment.

To deactivate the virtual environment, simply run the command:

deactivate

Advantages

  1. One of the main advantages of using virtual environments is that they allow you to manage dependencies for different projects separately. This can be especially useful when working on multiple projects that have different requirements or when working with different versions of the same package.
  2. Another advantage is that virtual environments allow you to test your code in different environment without interfering with your system-wide python installation, this will help you to avoid conflicts and unexpected behaviors.

In summary, Python virtual environments are an essential tool for managing dependencies and isolating different projects on a single machine, it allows developers to work on multiple projects with different requirements, and also to test their code in different environments without interfering with the system-wide python installation.

Cheers!

--

--

Amit Randive

I like to know something about everything and everything about something| Write to express and share