An introduction to python virtual environment - getting to know about your first python virtual environment

/media/article-img/python-virtual-env.jpg

What is a Virtual Environment?

A virtual environment in python is like a tool that helps create an isolated environment for each project to maintain the dependencies of different site-packages (3rd party libraries) required for each project. It is an essential tool for python developers that save time and complexities to avoid the version conflict of different site packages. 

 

Why do we need a virtual environment?

When you work with a single python project, you don’t need to worry about a virtual environment to maintain site packages for your project. But when you start working on multiple projects, you should think about managing site-packages. It is important. Imagine you have two python projects – Project A and Project B. In Project A, you work on Django version 2.0, and in Project B, Django version 3.0. How can you maintain these two versions of Django for both projects while the site packages, by default, for each project, use the same directories of your system machine to store and retrieve packages? That’s why we need a virtual environment. The virtual environment will help us create separate projects, A and B environments. You can install and use the project-specific site packages in each virtual environment. It does not matter which version of site packages you use in each environment. It works independently, and one environment will not affect another environment. You can create as many virtual environments as you need since they’re just directories containing a few scripts. Let’s create a virtual environment.

 

 

Step -1: Check the python version 

To work in a virtual environment, you must install python in your system machine. So, let’s check which version of python you are using. 

Open your command prompt (cmd) in a windows or Linux terminal and type -

python --version

It will show you the python version if it is installed on your system. If python is not installed in your system, it will go through an error. You can download and install the latest version of python from https://www.python.org/.

Let’s check the path of the python installation directories by typing -

which python

t will show you the location path of the python installation directory.

 

Step – 2: Install virtualenv

If you are working on python 2, you need to install virtualenv to create a virtual environment for your project. We are using pip to install virtualenv. Usually, pip is automatically installed when you install python. If pip is not working, you can manually install it. To install pip, follow this guideline - https://pip.pypa.io/en/stable/installation/

Let’s check the pip version and upgrade it to the latest version.

# Check pip version
pip -V

# Upgrade pip in windows
py -m pip install --upgrade pip

# Upgrade pip in Linux
python -m pip install --upgrade pip

Now, install virtualenv using pip. 

pip install virtualenv

If you are using python 3, you may not need to install virtualenv because python 3 has a built-in venv module from the standard library installed.

 

Step -3: Create a virtual environment

First, navigate to the folder/directory where you want to set up your python project using the cd command. Now, create an empty directory/folder.

# Create an empty directory 
mkdir python_demo_project
cd python_demo_project

Create a new virtual environment inside this directory.

# Python 2 
## virtualenv <name_of_your_virtual_environment>
virtualenv python_demo_project_env

# Python 3
## python3 -m venv <name_of_your_virtual_environment>
$ python3 -m venv python_demo_project_env

You will see a directory/folder name python_demo_project_env is created inside your project directory/folder.

 

Step – 4: Activate and deactivate the virtual environment

To use this environment, you need to activate it first. After that, you can install the necessary packages for your project.

# Windows
$ python_demo_project_env\scripts\activate
(python_demo_project_env) 

#Linux
$ source python_demo_project_env/bin/activate
(python_demo_project_env) 

To deactivate this environment, type deactivate in the command line.

deactivate

 

Step - 5: Installed a new package inside the virtual environment 

After activating your virtual environment, install a new package using pip.

pip install Django

It will install the latest version of Django compatible with the python and virtual environment version installed on your system machine. If you want to install a specific version, you can define it when installed.

pip install Django==3.0

 

Share To

About Author
  • Kamal Hosen
  • Kamal Hosen
    Geospatial Developer | Data Science | Python

    A passionate geospatial developer and analyst whose core interest is developing geospatial products/services to support the decision-making process in climate change and disaster risk reduction, spatial planning process, natural resources management, and land management sectors. I love learning and working with open source technologies like Python, Django, LeafletJS, PostGIS, GeoServer, and Google Earth Engine.