Hunnar Blog

Hunnar Blog

Creating Virtual Environment in Python

Hammad Ul Haq's photo
Hammad Ul Haq
·May 9, 2022·

5 min read

Creating Virtual Environment in Python

Photo by Kaitlyn Baker on Unsplash

Subscribe to our newsletter and never miss any upcoming articles

  • What Are Virtual Environments?

  • Importance of Virtual Environments

  • How to Use Python Virtual Environment

  • How to Use Conda Virtual Environments

What Are Virtual Environments?

A Python environment consists of two essential components: the Python interpreter that the virtual environment runs on and a folder containing third-party libraries. Any changes on dependencies installed in a virtual environment don't affect the dependencies of the other virtual environments or the system-wide libraries. Thus, we can create multiple virtual environments with different Python versions, plus different libraries or the same libraries in different versions.

Importance of Virtual Environments

The importance of virtual environments becomes apparent when we have various Python projects on the same machine that depend on different versions of the same packages. If working on two different machine learning projects that use the sklearn package, one using version 1.0.0 and the other using version 1.0.2. This would cause compatibility issues because Python cannot use multiple versions of the same package at the same time. The other use case that magnifies the importance of using Python virtual environments is when you're working on managed servers or production environments. Basically, setting up virtual environments is the best way to isolate different Python projects, especially if these projects have different and conflicting dependencies. Best practice is to always set up a separate virtual environment for each Python project and install all the required dependencies inside it — never install packages globally.

How to Use Python Virtual Environments

In this part of the tutorial, we'll learn how to create and activate virtual environments. Let’s overview the Folder Structure of the Python virtual Environment

venv/
├── Include
├── Lib
│   └── site-packages
├── Scripts
│   ├── Activate.ps1
│   ├── activate
│   ├── activate.bat
│   ├── deactivate.bat
│   ├── pip.exe
│   ├── pip3.10.exe
│   ├── pip3.exe
│   ├── python.exe
│   └── pythonw.exe
└── pyvenv.cfg

This tree structure gives a better view of what’s going inside the virtual environment.

  • Include: is an initially empty folder that Python uses to include C header files for packages you might install that depend on C extensions.

  • Lib: contains the site-packages\ folder, which is one of the main reasons for creating your virtual environment. This folder is where you’ll install external packages that you want to use within your virtual environment. By default, your virtual environment comes preinstalled with two dependencies, pip and setuptools. You’ll learn more about them in a bit.

  • Scripts: contains the executable files of your virtual environment. Most notable are the Python interpreter (python.exe), the pip executable (pip.exe), and the activation script for your virtual environment, which comes in a couple of different flavors to allow you to work with different shells. In this tutorial, you’ve used Activate.ps1, which handles the activation of your virtual environment for Windows PowerShell.

  • pyvenv.cfg: is a crucial file for your virtual environment. It contains only a couple of key-value pairs that Python uses to set variables in the sys module that determine which Python interpreter and which site-packages directory the current Python session will use.

Lets start creating virtual environment first make a project folder where you want to create project and create a virtual environment inside it. To do it, open the terminal, write the following command, and hit enter.

For Windows:

python -m venv venv

For Linux:

$ python3 -m venv venv

Activating a Python Virtual Environment

For windows:

venv\Scripts\activate.bat

For Linux"

$ source venv/bin/activate

Installing Packages:

For Windows:

python -m pip install <package-name>

For Linux:

$ python -m pip install <package-name>

Installing requirement :

python -m pip install -r requirements.txt

Deactivate a Python Virtual Environment:

It is very simple to deactivate python virtual environment just type deactivate

For windows:

deactivate

For Linux:

$ deactivate

Printing all packages installed in virtual environment

pip freeze

Update the Core Dependencies:

upgrading pip for Windows an Linux:

[path] pip install --upgrade pip

Upgrading dependencies for Windows

python -m venv venv --upgrade-deps

Upgrading dependencies for Linux:

$ python3 -m venv venv --upgrade-deps

Cleaning Virtual Environment

pip uninstall -y -r <(pip freeze)

Exporting and Importing Python Virtual Environment:

You can export the list of all the packages installed using

pip freeze > requirements.txt

Then push the requirements.txt file to anywhere you want to deploy the code, install the dependencies using pip install -r requirements.txt

pip install -r requirements.txt

Deleting Python Virtual Environment:

There is no command to remove virtual environment , you can deactivate it or remove the folder but unfortunately virtual environment library doesn't contain any kind of removal functionality.

How to Use Conda Virtual Environments

Conda is a language-agnostic tool for package management and environment management. As a package manager, Conda can install, update and remove packages. As an environment manager, it can manage virtual environments.

To create conda environment, create the project folder, open terminal:

For windows:

conda create --name myenv

myenv is the name of environment you can mention any name, You can also specify which version of Python you want to work with:

conda create --name my-env python=3.8

You can list all the available environments with

conda info –envs

Or

conda env list

Installing and updating any package in conda enviorment: Install any package in conda environment using below mention command

conda install [PACKAGENAME]

Update any package

conda update [PACKAGENAME]

Removing single or multiple package

Remove one or more packages (toolz, boltons),from a specific environment (bio-env).

conda remove --name bio-env toolz boltons

Removing conda enviorment

First you need to deactivate conda environment

conda deactivate

then run below command, mention environment name in place of ENV_NAME

conda env remove -n ENV_NAME

Cloning conda enviorment

You can make an exact copy of an environment by creating a clone of it:

conda create --name myclone --clone myenv

Replace myclone with the name of the new environment. Replace myenv with the name of the existing environment that you want to copy.

References:

https://realpython.com/python-virtual-environments-a-primer/
https://docs.python-guide.org/dev/virtualenvs/
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

 
Share this