What is a Virtual Environment in 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 allowing you to install different versions of Python and other packages different than your system version.
In this Python VENV tutorial I will show you how to manage different projects that might require different versions of packages or modules by creating a virtual environment.
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.
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
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
You will now observe that your prompt changes
I am now working within an isolated environment. First let’s see what version of Python we are running with 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
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
Create Python Virtual Environment using virtualenv // Video
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.
Frequently asked questions
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
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
- What is a Virtual Environment in Python?
- How do you create a virtual environment in Python 3 using venv?
- To create venv use the command
- How to exit VENV
- Create Python Virtual Environment using virtualenv // Video
- Frequently asked questions