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