Codementor Events

Understanding Virtual Environments in Python

Published Jul 06, 2020Last updated Jul 07, 2020
Understanding Virtual Environments in Python

More often than not, as a programmer, you will be required to work on different projects. These projects would also have different dependencies.

Let's say you're building two Python application simultaneously. Each of these applications have their own set of dependencies of Python version and packages.

One of them is a To-Do list app written in Python3 version and uses Django Rest Framework and another one is a Music Library written in Python2 version using Requests library and different/older version of Django to fetch music information from SoundCloud API.

Screen Shot 2017-10-15 at 6.32.46 PM.png

As a Python developer, you need to develop apps in a way that both of these applications are developed simulatenously and do not interfere with each others dependencies. This is where the concept of Virtualenvs comes to rescue.

What's Virtualenv

Screen Shot 2017-10-20 at 4.13.40 PM.png

Virtualenv is a tool which helps you create isolated python environments. Run following command in your terminal to install virtualenv.

pip install virtualenv

Creating a Virtual Environment for your project

virtualvenv venv

This command creates a virtual environment named venv for your project.

Activate Virtual Environment

source venv/bin/activate

This ensures that your new virtual environment is created and does not interfere with the host operating system.

Feel free to create a project with a virtual environment. If you're stuck, feel free to reach out to me. I will be happy to help.

Discover and read more posts from Priyank Trivedi
get started