Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating a Virtual Environment

Give Feedback

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>

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

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!

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?

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.

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?

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.

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!

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)…

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.

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!

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.

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


python3 -m venv FOLDER_NAME

(does not work)

To activate, try

source FOLDER_NAME/Scripts/activate

Thanks for the great series of video tutorials.

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 :)

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

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.

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?

viraj7k on May 8, 2020

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

Become a Member to join the conversation.