• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Roger Perkin

Network Automation Architect

  • Network Automation
    • Network Automation Courses
    • What is NetDevOps?
    • Workflow Orchestration
    • Ansible Automation Platform
    • Ansible Workshop
    • What is Network Automation?
    • Network Automation Tools
    • ContainerLab
    • Ansible Training
      • What is Ansible?
      • Ansible Tutorial for Beginners
      • Ansible Network Automation
      • Ansible Hosts File
    • Python Network Automation
      • Nornir
      • Python Network Automation Course
      • Python for Network Engineers
      • Python VENV / Virtual Environment Tutorial
      • Python Tutorial for Beginners
      • pyATS
    • Network Source of Truth
      • NetBox Training
      • Infrahub
    • NetDevops
    • DevOps Tutorial
      • Git Training
      • Terraform Training
      • Linux Training
      • Kubernetes Training
      • Devops Training Course
      • Azure Devops Training
    • Terraform
    • GIT
      • Git Commands
      • What is GitHub?
    • Docker Training
    • Confluence
    • Microsoft Azure
  • Cisco
    • ISE
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • CCIE
  • Blog
  • About
    • My Red Special Guitar
  • Contact

Python VENV / Virtual Environment Tutorial

Home » Network Automation » Python Network Automation

What is a Python Virtual Environment?

Basically a Python virtual environment is a folder with a few scripts in it that allows you to create an isolated environment to work on your project allowing you to install different versions of Python and other packages different than your system version.

In this Python virtual environment tutorial I will show you how to manage different projects that might require different versions of packages or modules by creating a virtual environment.

The module used to create a virtual environment is VENV.
VENV will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running Python3 or whichever version you want.

Python VENV vs Virtualenv

Python VENV & Virtualenv are nearly interchangeable, the main difference is virtualenv supports older versions of Python i.e 2.x and VENV is now the standard library in Python 3

This will all be done on Ubuntu but the principles are the same for other operating systems

By default all packages or modules are installed in the same location (this location is dependent on your system).

If you are writing a project that requires version 1.0 of a certain package and another project that requires version 1.5 of a certain package you have a bit of a problem.

This is where a Python virtual environment comes in.

There is no limit to how many virtual environments you can have as it’s just a folder.

You can use virtualenv or venv to create this virtual environment.
Both perform the same job but venv is installed by default with Python3 and virtualenv has to be installed as a package.

If you are running Python2 you need to install virtualenv, if you are running Python 3 then venv is the way to go.

Python create venv

How do you create a virtual environment in Python 3 using venv?

First I would recommend you create a folder on your system where you have all your virtual environments.

mkdir VENVS 
cd VENVS 

To create venv use the command

python3 -m venv <name of your environment> 

So if I wanted to create a virtual environment called env1 I would type

python3 -m venv env1

If I now do an ls in my VENVS folder I will see another folder called env1

python virtual environment using venv create

To use this you now have to go into the folder and activate it, (start virtual environment python) this will setup your shell so use the environments Python executable and it’s site packages by default.

How to Activate Python Virtual Environment

From within my VENVS folder I enter the command

source env1/bin/activate

You will now observe that your prompt changes

python activate venv

I am now working within an isolated environment. First let’s see what version of Python we are running with which python

which python

We actually are running an isolated version of Python from within this folder.

Then we will see what packages we have installed with pip3 freeze

pip3 freeze in venv

You can see there are none – compared to if we exit the venv this list would have all the packages.

How to exit VENV

To exit a virtual environment you use the command deactivate

python venv deactivate

Best Python Virtual Environment Tutorial

If you are just starting out learning Python you might not have a need to create a virtual environment, but as you write more code and different projects with different dependencies, you will find you are using the venv command every day! The best way or tool to create a virtual environment is really the one that works for you.

I use Python for Network Automation if you like these posts and want to learn more check my other posts.

Python virtual environment VSCode

tbc

Frequently asked questions

Is VENV in standard library?

The VENV module is part of Python’s standard library, and it’s the officially recommended way to create virtual environments since Python 3.5

Is VENV installed with Python?

VENV comes installed with the Python standard library and does not require any additional installation.

Git / Github version control

What is a virtual environment in Python?

In simple terms a virtual environment is a folder where you can run an isolated environment so you can install different versions of Python and or packages and libraries, so they do not interfere with your system versions of the same packages.

Is VENV the same as virtualenv?

They are nearly interchangeable, the main difference is virtualenv supports older versions of Python i.e 2.x and VENV is now the standard library in Python 3

How do I activate VENV in Python?

From within your virtual environment folder run the following command source env1/bin/activate (in this example our virtual environment is called env1)

What are the two main advantages of using Python virtual environments?

There are several advantages of using Python virtual environments, but the two main advantages are:
Isolation of Dependencies: One of the main advantages of using Python virtual environments is that they provide a way to isolate dependencies for different projects. This means that you can create separate virtual environments for each project, each with its own set of packages and dependencies. This ensures that the packages installed in one project don’t conflict with packages installed in another project. This is especially important when you are working on multiple projects, as it can be difficult to keep track of which version of a package is required for each project. By using virtual environments, you can ensure that each project has access to the specific packages and dependencies it needs without affecting other projects.

Reproducibility: Another advantage of using virtual environments is that they help to ensure reproducibility. When you create a virtual environment, you can specify the exact versions of the packages and dependencies required for your project. This means that if you share your project with someone else, they can create the same virtual environment and run your project with the exact same package versions. This helps to avoid issues with compatibility and ensures that your code works consistently across different environments. Additionally, virtual environments make it easy to switch between different versions of Python itself, allowing you to test your code on different versions of Python to ensure maximum compatibility.
In summary, Python virtual environments provide a way to isolate dependencies and ensure reproducibility, making it easier to manage multiple projects and share code with others. By using virtual environments, you can avoid conflicts between different versions of packages and ensure that your code works consistently across different environments, helping to streamline your development process and make it more efficient.

Learn more about Python

  • 7 Best Python Network Automation Videos on YouTube
  • Best way to learn Python
  • Nornir Python Automation Course
  • Python VENV Tutorial
  • Python Scripts for Network Engineers
ansible course for network engineers
Category: Python Network Automation
ansible course for network engineers
Get Access to my Ansible Course NOW
Previous Post:cisco ise upgradeHow to upgrade Cisco ISE 2.4 to 2.6
Next Post:Network Automation with Pythonpython scripts for network engineers featured image

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Sidebar

Hi I'm Roger Perkin,
Based in the UK working as a Network Automation Architect, CCIE #50038
About Roger | Twitter | Linkedin

python course for network engineers

More Python

  • Python Network Automation
  • Cisco pyATS Tutorial for Beginners
  • Network Automation Certification
  • Network Automation with Python
  • Python VENV / Virtual Environment Tutorial

Topics

Network Automation
Ansible
Python for Network Automation
CCIE
Cisco ISE
F5 Certification
BGP
OSPF
Network Automation Conferences
auvik promo banner
Pluralsight Trial

Git for Network Engineers

Ansible vs Nornir

Start learning today with my Network Automation Courses

Master Ansible, Python, Git, Nornir, Jenkins and more..


Buy me a coffeeBuy me a coffee

ansible network automation course

Have you seen my YouTube Channel?

YouTube Subscribe

Let’s get started

Take a look at my premium courses on Ansible, Nornir & Git or buy them all with the Network Automation Bundle!

Network Automation Courses

Navigation

Python VENV Tutorial
Python for Network Engineers

Network Automation
Network Automation Courses
Network Discovery Tools
Network Automation Conferences
Ansible Training
What is Ansible?
Devops Tutorial
Network Source of Truth
DevOps Glossary
Network Monitoring Software

Contact

Contact

Get in touch with me here

[email protected]

  • Twitter
  • LinkedIn
  • YouTube
Buy me a coffeeBuy me a coffee

Copyright © 2025 · Roger Perkin · All Rights Reserved · Privacy Policy – Terms