Udit Vashisht
Author: Udit Vashisht

Venv Python - A complete tutorial on Virtual Environments in Python

  • 4 minutes read
Venv Python - A complete tutorial on Virtual Environments in Python

    Table of Contents

What is Virtual Environment in Python ?

Virtual Environment is a kind of a container which runs specific version of Python and its modules. And it is used in the cases where you are working on two different project which has different dependencies. Say one of your project uses Django 1.11 and another one uses Django 2.0. In that case you will be needing Virtual Environment.

Why do we need to use Virtual Environment in Python?

There are cases when you are working on different projects and you want to have different versions of Python for different project say Python 3.7 and Python 3.8. Using, virtual environments, you can use different versions of Python as well as the modules installed in it. E.g. in Kivy supports only upto version 3.7 but the latest version of Python is 3.8 so in order to install and run Kivy I have used Virtual Environment to run Python 3.7 in this tutorial video of kivy in which you can learn to build a stopwatch in Python using Kivy.

For the purpose of this tutorial, I will be using MacOS but you can replicate the same on Windows and Linux using PowerShell and Command Line.

Virtualenv vs Venv? How to install Virtualenv?

Prior to Python 3 i.e. in Python 2.7, you will have to install the module called virtualenv to create virtual environments in Python. It is a third party module and you can still install virtualenv in Python 2.7 as well as Python 3 and above using pip.

pip install virtualenv

But Python 3 and above, ships with an inbuilt module called venv, which solves the same purpose as of virtualenv i.e. it helps to create and use virtual environments in Python.

How to create a Virtual Environment in Python?

We will be using venv to create virtual environment in Python as I assume that all of you are using Python 3 as the support for the legacy version i.e. Python 2.7 has already ended. However, if you are still using Python 2.7 just replace venv with virtualenv in all the codes to create a virtual environment in Python 2.7.

Now, to create a virtual environment in Python using venv your will have to use the following code :-

python -m venv name-of-the-env

How to create a Virtual environment using different versions of Python?

If you want to create a virtual environment using a specific version of Python then first of all you must have that version installed in your computer and then you will have to check how you run that version of python e.g. in my system I can run Python 3.7 and Python3.8 using python3 and python3.8 respectively in the terminal.

So in order to create a Virtual Environment using Python version 3.7, I will have to use :

python3 -m venv name-of-the-env

**You must note that you can use the same code in Windows and Linux to create the Virtual Environments.

How to activate a Virtual Environment in Python?

In order to activate a virtual environment in Python, you will have to use the following command on MacOS and Linux

venv name-of-the-env/bin/activate

However, to activate the Virtual Environment on a Windows Machine, you will have to use the following command:-

. .\name-of-the-env\Scripts\activate

How to deactivate the Virtual Environment in Python?

To deactivate the Virtual Environment on all the operating systems like MacOS, Linux and Windows, you just have to type deactivate in the terminal/powershell/command line.


Shortcut to create a Virtual Environment in Python

Instead of naming the venv/virtual environment everytime, you can create a virtual environment with the name of the directory in which you are working by using a period (.) at the end. So, in order to create such virtual environment on all the operating systems, you will have to type the following command :-

python3 -m venv .

And to activate this on MacOS and Linux

source bin/activate

On Windows:

. .\Scripts\activate

Video Tutorial on Virtual Environment (VENV) in Python

Don’t forget to check out our video tutorial on Venv in Python

Related Posts

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4
By Udit Vashisht

Matplotlib Tutorial in Python

*Matplotlib Stack Plots/Bars | Chapter 4

In this chapter we will learn to create Matplotlib/Python Stack Plots/Bars.Stack Plots are also called area graphs/plots in general.

Matplotlib Tutorials in Python - Creating a Simple Stack Plot in Matplotlib

A good example of using a stack ...

Read More
Matplotlib Candlestick Chart in Python | Matplotlib Tutorial | Chapter 11
By Udit Vashisht

How to create a Matplotlib Candlestick Chart in Python?

A candlestick chart or Japanese candlestick chart is a financial chart used to depict the price movement of securities, derivatives etc. in financial market. We can create a Matplotlib Candlestick Chart using a module called mpl_finance, which consists of ...

Read More
Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 3 | Creating Pie Charts in Python

In this chapter, we will learn to create Pie Charts in Matplotlib in Python. Pie Charts are an intiutive way of showing data, wherein each category is shown as a portion of the pie. Let ...

Read More
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append