A Quick Guide on How to Setup a Python Virtual Environment [Windows, Linux & Mac]
This post was originally published on my blog — https://thecodingpie.com
A virtual environment is a tool that helps to keep your current project and all its dependencies isolated from your main system and the other projects you are working on.
To make it a bit clearer imagine this scenario — you have two Django projects namely ProjectOne
and ProjectTwo
. Unfortunately, your ProjectOne
wants to run in Django 1.0, and your ProjectTwo
needs Django 3.0. How would you do that? If you install Django globally on your system, then it would be impossible to satisfy the above condition. And here’s where the concept of virtual environment comes in handy. If you set up a virtual environment for both of your projects, then each project and all its dependencies would be isolated from each other in its own virtual environment and both won’t get mingled. So your ProjectOne
can use Django 1.0 in its own environment and ProjectTwo
can work in Django 3.0. Thus we can safely work on our project at any time without any worry! Got that?
It’s always a good practice to create a new virtual environment for every new Python project you work on.
There are so many ways you can create a virtual environment by using different python modules like:
- virtualenv
- pyvenv
- venv
But the one we are going to use is the venv module. If you are using a newer version of Python, then venv module is the preferred way to create and manage virtual environments. Fortunately, venv is included by default in Python 3.4 and greater.
So Before moving forward please satisfy the following requirements then only continue:
- You should have the latest version of Python installed (anything greater than Python 3.4).
- You should have added Python to your environment PATH variable.
- You should have venv installed. You don’t have to worry about this if you are using the latest version of Python.
If you satisfy all those 3 conditions, then you can head on to creating the virtual environment, Otherwise please do them first then continue.
With those things in place, now you are ready to install and activate a Python3 virtual environment using venv in Windows, Linux, and macOS.
So let’s do that…
Step 1: Creating the Virtual Environment
Now we are all on the same page, we all have the latest version of Python (or at least 3.4+), we have added Python to the environment PATH variable and we have venv installed.
It’s time to create a virtual environment.
- Open up your terminal window, and navigate to the folder where you want to set up the virtual environment. Then type:
python3 -m venv name_of_your_venv
Remember to replace name_of_your_venv
with whatever name you would like to give to your virtual environment.
- Now you should have a directory with the name you gave like this:
Note: I always create the venv folder inside the root directory of my project which I am working on. For example, if my project’s root directory’s name is new_project like above, then I would create the virtual environment folder directly inside my root folder. Some people create files inside venv folder, Don’t do that, Don’t create your project files inside the venv folder. Instead, keep this venv folder as a sibling folder and create your project files and other folders outside this venv folder like this:
Step 2: Activating your virtual environment
Make sure you are still in the same directory within your terminal.
Activating the virtual environment would be different according to your OS and the shell you are using, so do anyone of the following that match your specs.
Windows
- If you are using cmd then do this:
name_of_your_venv\Scripts\activate.bat
- If you are using PowerShell, then:
name_of_your_venv\Scripts\Activate.ps1
- And If you are using PowerShell Core, then:
name_of_your_venv/bin/Activate.ps1
Linux/Mac
- If you are using Linux/Mac and if you are using bash/zsh, then type the following:
source name_of_your_venv/bin/activate
- If you are using fish, then:
source name_of_your_venv/bin/activate.fish
- If you csh/tcsh, then:
source name_of_your_venv/bin/activate.csh
That’s it!
If you have done all the above steps correctly, then you would see something similar to this:
Now you can install whichever dependencies you want and it will be isolated in this virtual environment you now created.
- Finally, if you want to leave your virtual environment, then simply type:
deactivate
Now that prefix would have gone.
Wrapping Up
Hope this guide helped you to set up your own virtual environment. And always remember to use a new virtual environment for every new Python project you work on.
If you got any errors, then please comment on them below.
Thank you!