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

Roger Perkin

Learn Network Automation

  • Network Automation
    • Ansible
    • Python
    • Terraform
    • pyATS
    • Git
  • ISE
  • Cisco
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • Blog
    • CCIE Blog
  • COURSES
  • Menu Item

Python Virtual Environment Tutorial

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 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 the Python virtual environment comes in.

It allows you install certain versions of packages within the virtual environment which is specific to your project.

What is a Virtual Environment in Python?

Page Contents

  • What is a Virtual Environment in Python?
  • How do you create a virtual environment in Python 3 using venv?
    • Activate Python Virtual Environment
  • Create Python Virtual Environment using virtualenv // Video
  • Conclusion
    • Learn more about Python

Basically a virtual environment is a folder with a few scripts in it that allows you to create an isolated environment to work on your project.

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 Python 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.

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 

Then to create your environment 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 venv create

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

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 isolate 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.

To exit a virtual environment you use the command deactivate

python venv deactivate

Create Python Virtual Environment using virtualenv // Video

Conclusion

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!

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

Learn more about Python

  • 7 Best Python Network Automation Videos on YouTube
  • Best way to learn Python
  • Nornir Python Automation Course
  • Python Virtual Environment Tutorial
  • Python Scripts for Network Engineers
Category: Python Network Automation
Previous Post: « cisco ise upgrade How to upgrade Cisco ISE 2.4 to 2.6
Next Post: 7 Python Scripts for Network Engineers python scripts for network engineers featured image »

Reader Interactions

Leave a Reply Cancel reply

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

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

Social

Follow along on social media

Contact

Get in touch with me here

[email protected]

Navigation

Home

Blog

About

Contact

YouTube

Don’t forget to take a look at my YouTube Channel

youtube button

Copyright © 2021 · Roger Perkin · All Rights Reserved · Powered by Mai Theme