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>

Comments & Discussion

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!

Become a Member to join the conversation.