A Quick Guide on How to Setup a Python Virtual Environment [Windows, Linux & Mac]

Confused about How to set up a Python virtual environment like venv in Windows/Linux/Mac? Then you are in the right place. In this Quick Guide, you will learn how to create, and activate a Python virtual environment in 2 easy steps.

thecodingpie . . 4 min read . 872 Hits

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:

  1. You should have the latest version of Python installed (anything greater than Python 3.4).
  2. You should have added Python to your environment PATH variable.
  3. 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:
my venv folder
I named my virtual environment "venv"

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:

other folders and files as a sibling folder to venv folder
You don't have to create these files and folders

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:

final image that shows the name_of_your_venv as prefix in terminal windows
If you see the prefix with the name_of_your_venv, then you have successfully activated the virtual environment

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!

About Me

Hey folks, my name is Aravind, and I am the man behind this website. To know more about me, check out the About Me page. If you like and enjoy my content, then please consider supporting what I do through - Buy Me a coffee.

Comments(0) No Comments Yet :(
Leave Your Comments

Similar Posts

Let's Build a Web Scraper with Python & BeautifulSoup4

. 15 min read . 1.5K hits

How to build your own Python Voice Assistant | thecodingpie

. 10 min read . 7.3K hits

10 Best Python Courses Online | Learn Python Online | 2020

. 13 min read . 2.3K hits

Make your own text based adventure game in Python3

. 13 min read . 2.2K hits

Number guessing game | Python3

. 8 min read . 1.5K hits