Loading video player…

Creating a Virtual Environment

This lesson covers how to create a virtual environment in a project folder. You learned that following the steps below will install a self contained Python environment in your project directory:

  • Create a project directory
  • Change into the project directory
  • Run python3 -m venv <name_of_virtualenv>

00:00 So, first of all, we’re going to take a look at where the global environment currently lives, and we can do that with the which command. In this case, I’m going to be using Python 3, which means that I’m going to add this 3 suffix to all of my commands here, like pip3, python3, and so on. All right, so the first thing that we’re going to do is we’re going to take a look at where the global environment currently lives.

00:25 And I’m going to do that with the which command, which tells me the path to a file or to an executable. And in this case, I’m looking at the pip3 command because I’m using Python 3 here.

00:37 So if I run this, it tells me that my Python install lives inside /usr/local/bin/—or, my pip install lives inside /usr/local/bin/. This is typical if you’ve installed that through homebrew on macOS, for example.

00:50 Now, I’m going to pretend I’ll be working on a new Python project here, so I’m going to create a new directory for this project, and then I’m going to change into this project directory.

01:02 And what I’m going to do now is I’m going to create a virtual environment inside this folder here. So, what I’m going to do here, I’m going to use the magic incantation python3 -m venv, which stands for virtual environment, and I’m just going to tell it to create a virtual environment inside this my-python-project/ folder, and I want it to create that inside a new subdirectory called venv, which is just a naming convention that I like to use.

01:30 And this is going to set up a new virtual environment. This should finish any minute now. All right, so we just created the new virtual environment, and now when I look at the contents of my project folder, previously it was empty and now I’ve got this venv/ folder in there, and then when we look inside it, you can see there’s a bunch more directories and pretty much, like, a full Python install. And we can drill down further just to show you that there’s a bunch of stuff in this folder here!

02:00 So, when we just list a directory tree here, you can see that we’ve got this bin/ folder, this include/ folder, lib/ folder.

02:09 This is where all of our Python third-party packages are going to live, and they were seeded with some basic stuff, like a very basic Python install. And that’s what a virtual environment does. Now we’ve pretty much got this tiny self-contained virtual environment—a Python environment—installed inside our project folder.

Avatar image for Andreas Schipplock

Andreas Schipplock on April 7, 2019

Hi, is this still considered a best-practice to create the venv inside the project? I somewhere read it’s advised to create all of your venvs inside ~ (or $HOME).

I’m just curious :).

Thanks in advance.

Regards, Andreas

Avatar image for Dan Bader

Dan Bader RP Team on April 8, 2019

@Andreas: I still create all of my venvs that way, I think it’s more of a “philosophical” point as to which practice is better (venv in project folder vs venv in global folder like ~/venvs).

What I would avoid is having shared global venvs that are used by multiple projects. It might make sense under some circumstances, like when you have several scripts/apps that all use the same set of dependencies.

But over time and as these projects evolve it can become a hassle to keep the shared venv clean. I find per-project venvs much easier to manage over the long term.

Hope this helps you out!

Avatar image for paulakula11

paulakula11 on May 22, 2019

It would be helpful to mention if these tutorials are platform dependent. The first instruction to create a virtual environment failed. An error message stating that python3 -venv needs to be installed first with the command sudo apt-get install python3-venv.(ubuntu linux platform|) I did this before creating a new directory and crerating a virtual environment as given in the tutorial. I am just wondering ,how will this work in windows?

Avatar image for Dan Bader

Dan Bader RP Team on May 23, 2019

@paulakula11: Thanks for pointing out that Ubuntu requires installing the python3-venv package :-)

Python 3 installs on Windows should include the venv module by default, so the python3 -m venv FOLDER_NAME command should work out of the box.

Avatar image for Ahmad Mayahi

Ahmad Mayahi on May 23, 2019

Hi Dan, just a qucick question, I see many people are installing venv as follows:

python3 -m venv venv3

So, they specify the version of the venv module, is that considered a best practice? or they just want to be more percise?

Avatar image for Dan Bader

Dan Bader RP Team on May 23, 2019

@Ahmed: I’ve seen that too where someone would name the virtual env folder venv2 venv3 (or even more granular like venv35, venv37, …) depending on the Python version.

It might make sense if you’re running or testing a project on multiple Python versions. I typically only use a single venv folder so the steps to activate the virtual environment are the same between all of my projects. When I’m testing with multiple versions of Python I’ll use something like tox to automate the process.

Avatar image for Jet

Jet on June 13, 2019

Really great to find so many good tutorials on important topics. The only thing I keep running into: it is almost always not clear what version of Python (or Django, or ....) is used. That can make a lot of difference re. commands you should use etc. It would save students so much time if this information would be given upfront. Now, I often follow a tutorial for 10 mins or so, only to find out it uses Python 2 instead of 3, or Windows instead of OS X, and then I quit. Especially for beginners this information can be very helpful!

Avatar image for csharma19

csharma19 on Aug. 3, 2019

Thanks for making this video…what additional steps would be needed if I’m using VS Code (via Anaconda)? I keep running into all sorts of issues (haven’t even gotten to testing the set up for a virtual environment yet....but am hesitant to try before I fix the existing issues)…

Avatar image for hung20736

hung20736 on Dec. 9, 2019

I can’t do: python3 -m venv ./venv. It triggers

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/hungnguyen/vevExample/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

When i do : apt-get install python3-venv. It again triggers

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

Im using Windows Subsystem for Linux.

Avatar image for bmorton

bmorton on Dec. 28, 2019

Hi Dan, Can you or someone else on your team make a complete virtual environment tutorial for Windows? And maybe incorporating Anaconda/Jupyter Notebooks? It seems different enough to merit its own video. Thanks!

Avatar image for Dan Bader

Dan Bader RP Team on Dec. 29, 2019

@bmorton: That’s a great idea—I’d love to go back and expand this course or do a more Windows-specific version in the future.

Avatar image for bmorton

bmorton on Dec. 29, 2019

Great! Can you DM me on Slack when it is in the works? Been struggling with setting up a venv for a minute now, can’t find a good tutorial for Windows. Thanks!

For windows systems (Python 3.8), the lessons learned are:

python -m venv FOLDER_NAME

(works)

python3 -m venv FOLDER_NAME

(does not work)

To activate, try

source FOLDER_NAME/Scripts/activate

Thanks for the great series of video tutorials.

Avatar image for Dan Bader

Dan Bader RP Team on Jan. 13, 2020

Thanks @DJ, I’ve updated your comments as requested. Really appreciate that you went back and took the time to share your findings with us. It really helps other course participants :)

Avatar image for andresgtn

andresgtn on April 11, 2020

Great tutorial. I’m struggling to find one that shows best practices for virtual environments and git repos. Do you create the venv inside the repo or the repo inside the venv? How do you keep track of all dependencies and push them to the repo? I’m fairly new to all this environment management and want a way to keep projects clean. Maybe theres a tutorial in this site already which you could point me to. Thx

Avatar image for Ricky White

Ricky White RP Team on April 12, 2020

Typically you would run them side by side. If you put your .git folder in the venv, if anything happens to that, then you also loose your git history. They should be independent of each other. You would export your dependencies to a requirements.txt file which will be added to your source control. That way, if you or someone else needs to create a new venv they can import dependencies from the requirements.txt file.

Avatar image for Gregory Klassen

Gregory Klassen on April 17, 2020

I am perplexed about the anaconda virtual environment. If I loaded anaconda globally, that seems to have created it’s own virtual environment. Is that a good idea (given it’s size).? When I create virtual environments in addition to the anaconda virtual environment it feels messy - but is this what you are suggesting?

Avatar image for viraj7k

viraj7k on May 8, 2020

hey Dan, the command you use are same for windows? if different could you please mention it

Avatar image for tom77-ai

tom77-ai on Feb. 14, 2021

Hi Dan, most of the commands are not working for the windows shell using python 3.9 Is there an overview what works here?

Avatar image for Ricky White

Ricky White RP Team on Feb. 15, 2021

Hi tom77-ai.

Check the other comments above and you’ll see the Windows version of the commands you need.

Avatar image for John Paul CollabEdge

John Paul CollabEdge on May 29, 2021

What is the difference between (in ubuntu) virtualenv and python3 -m venv ? I see some tutorials using virtualenv and this one uses python3 -m venv. Does it matter which one I use?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on May 31, 2021

@John Paul CollabEdge virtualenv is a third-party library that used to be popular for managing virtual environments in Python. You can continue using it, but a subset of it has been integrated into the standard library under the venv module in Python 3.3, which is the recommended and portable way.

Avatar image for Charlie Grover

Charlie Grover on Sept. 11, 2021

I cannot activate using the responses up top of this post. I am running Python 3.9

In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:

$ source env/bin/activate (env) $

I have tried every combination of the python line to no avail. please help.

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 13, 2021

Hi @Charlie Grover, what operating system are you working on?

Check out the different commands for activating your venv described in the official Python documentation.

Avatar image for h602421365

h602421365 on Dec. 11, 2021

I use the command prompt and it says that which is not recognized

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 13, 2021

@h602421365 which is a shell command available on Unix-like operating systems, including macOS and Linux distributions like Ubuntu. If you’re on Windows, then you might try the equivalent where command.

Avatar image for VitaminC

VitaminC on Oct. 5, 2022

Does Windows have an equivalent to the ‘which’ command?

Avatar image for VitaminC

VitaminC on Oct. 5, 2022

Forget question above - found answer on a later page discussion.

Avatar image for Ranga

Ranga on April 3, 2023

For windows, the command to check python source folder is

pip3 --version.

python --version

only prints out the version of the global environment python, whereas

pip3 --version

prints out the folder in which pip is present.

To list down the folder structure after installation of the venv, try using the command

tree .

This will show the current folder and the installed venv folders and files.

Avatar image for Ranga

Ranga on April 3, 2023

A correction to my earlier comment.

tree.

will show only the folder structure.

Using

tree /f .

will show both folders and files.

Become a Member to join the conversation.