Virtual Environments in Python

Netra Prasad Neupane
4 min readMay 18, 2024

--

In software development, we are often stuck in situations where we need to switch/test on multiple versions of dependencies like opencv-python 2.4 and opencv-python 3.12. Recently one of my friends who is working as a Web Developer requested me to write on virtual environments because he is following my Conversational Streaming Bot using Gemini, Langchain and Streamlit article where I have recommended installing dependencies inside a virtual environment instead of a global installation. You know what, He doesn’t have any Python experience yet. Probably there might be other readers who are also struggling to setup a virtual environment like my friend. So, to help them and make it easier to follow my articles, I decided to write a step-by-step guide to create a virtual environment in Python.

How often have you faced a similar situation(testing your project on multiple version of dependencies) ? How do you tackle it?

  • Installing all the dependencies in the global environment by overwriting the existing dependencies configurations?

If you are following the above approach and you have just started the programming then it’s Ok. But if you are an experienced programmer you can’t do that because overwriting system dependencies might create dependency issues, becomes hard to restore the previous state of dependencies and it is also time-consuming tedious process. Let me explain my approach to address the above situations:

  • Creating a virtual environment(isolated environment from the existing global environment), and installing dependencies in the environment.

Introduction

A Virtual environment is an isolated environment created to manage dependencies, ensuring that each environment can have its own set of dependencies without interference from others. So, a Virtual environment is a way to manage the different versions of libraries and packages for multiple projects.

fig: virtual environment in python (source: https://www.boardinfinity.com/)

In Python there are mainly two ways to manage the virtual environments:

  • using venv module
  • using conda package manager

venv

The venv module supports creating lightweight “virtual environments”, each with its own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available[1]. To create, activate and install packages in the environment stick with the following steps :

  • Create virtual environment

To create the virtual environment using venv , first, go to the directory where you want to store the library/dependencies within the environment. Once you are in the destination directory then write the following command in the terminal:

python -m venv my_virtual_env

Here, I have created my_virtual_env environment, you can change the name of the environment according to your personal preference. Now, my_virtual_env/ directory will be created in your working directory.

  • Activate virtual environment

If you are in a window machine then, type the following command in terminal

my_virtual_env\Scripts\activate

If you are in Linux or macOS then, type the following command.

source my_virtual_env/bin/activate
  • Installing packages in a virtual environment

Once your virtual environment is activated, you can install the packages using pip package manager without affecting your global dependencies.

pip install dependency_name
  • Run Python program

Now, you can run your Python file inside your virtual environment just by typing python filename.py .

  • Deactivate virtual environment

To deactivate the virtual environment type deactivate command on the terminal which will return you to the global python environment.

conda

Conda is an open-source Python package manager which is originally developed to solve the package management challenges. You can install the conda package manager either using anaconda distribution or light-weight minicondadistribution. To create, activate and install the packages in the environment follow the steps given below :

  • Create virtual environment

To create the virtual environment using conda package manager, you should specify the version of Python along with the environment name.

conda create --name my_virtual_env python=3.12

Here, I have specified my_virtual_env as the virtual environment and the 3.12 version of Python to install on the environment. Now, it will create the env/ folder inside the distribution directory (miniconda or anaconda ) in your system where all the dependencies are installed inside the virtual environment.

  • Activate virtual environment

Activate the virtual environment by typing the following command in the terminal

conda activate my_virtual_env
  • Installing packages in a virtual environment

You can install desired packages either using pip or conda package manager.

pip install package_name
# or
conda install package_name
  • Run Python program

Now, you can run your Python file inside your virtual environment just by typing python filename.py .

  • Deactivate virtual environment

Deactivate the environment using

conda deactivate

Why I preferred conda for virtual environment management ?

Both venv and conda can be used to create a virtual environment in Python. However, there is a major difference in working principle between the above approaches: venv uses the Python interpreter installed on the system. It means you can’t install the desired version of Python in a virtual environment using venv but conda package manager let’s you to install the desired Python version inside your virtual environment. That’s why I preferred conda over venv.

If you have any queries I am happy to answer them if possible. If you liked it, then please don’t forget to clap and share it with your friends. See you in the next blog…

References:

  1. https://docs.python.org/3/library/venv.html
  2. https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#activating-an-environment

--

--

Netra Prasad Neupane

Machine Learning Engineer with expertise in Computer Vision, Deep Learning, NLP and Generative AI. https://www.linkedin.com/in/netraneupane/