DigitalOcean Referral Badge
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

Chapter 6 - Data Types & Variables
By Udit Vashisht

Variables and Identifiers in Python

If we go by the dictionary meaning ‘Variable’ is something which is ‘able to be changed or adapted’. Which is true to much extent in terms of Python programming language also. Variable is basically a reference to the memory location where an object is ...

Read More
How to fill area between the Line Plots? | Matplotlib Tutorial in Python | Chapter 5
By Udit Vashisht

How to fill area between the line plots in Matplotlib?

You can easily fill the area with any color between the lines or under a curve in Matplotlib Line Plots using plt.fill_between().

Parameters of matplotlib.pyplot.fill_between() or plt.fill_between()

The syntax for plt.fill_between() is :

matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, ...

Read More
If __name__ == '__main__' in Python
By Udit Vashisht

What does if __name__ == ‘__main__‘in python means?

In python, you must have come accross the following code :-

if __name__ == '__main__': main() 

In this tutorial, we will learn in detail what does the above statement means. As per official documentation:

‘__main__‘ is the name of the ...

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 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 raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python