Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Virtual Environments Recap

This lesson is a recap of the entire course, it covers the benefits of using virtual environments and best practices to follow when using them in your projects.

Take the Quiz: Test your knowledge with our interactive “Python Virtual Environments: A Primer” quiz. You’ll receive a score upon completion to help you track your learning progress:

Interactive Quiz

Python Virtual Environments: A Primer

In this quiz, you'll test your understanding of Python virtual environments. With this knowledge, you'll be able to avoid dependency conflicts and help other developers reproduce your development environment.

Avatar image for Robert T

Robert T on March 16, 2019

Good stuff, Dan. I now understand how it works in a Linux OS, it seems so clean compared to Windows with Pycharm. Everything you did was outside of the code editor. I’d really like to see how to accomplish the same thing under Windows and Pycharm.

Avatar image for Douglas Fabretti

Douglas Fabretti on April 10, 2019

Nicely explained, short and concise! Thank you very much for sharing this series with us Dan.

Avatar image for Andres Salgado

Andres Salgado on April 19, 2019

Thank you Dan!! Great course.

Do you care to elaborate on the difference between virtualenv and venv? I understand that based on a stackoverflow entry (stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe) virtualenv does not contain all standard libraries. Is that correct?

Avatar image for Dan Bader

Dan Bader RP Team on April 19, 2019

@Andres: Thanks! Basically virtualenv and venv do the same thing. You’ll get a fully self-contained Python environment including the full standard library.

The main difference is that venv is included with Python 3 out of the box and not available on Python 2.

So personally I use venv for all Python 3 projects, and I use virtualenv as a fallback for Python 2 projects where venv isn’t available.

Hope that helps you out :)

Avatar image for Sean Dockery

Sean Dockery on April 24, 2019

I am working with a Python 2 application and the inconsistency that venv is only available on Python 3 tripped me up. It wasn’t clear to me that venv wasn’t available for Python 2 or why I wasn’t able to install it. I wish that I had seen your most recent comment earlier. Otherwise, the course was direct and clear.

Avatar image for charliem22

charliem22 on May 28, 2019

Great job! I’m just staring my first ‘big’ project and needed to understand venv machinary. This tutorial was exactly what I needed. Thanks Dan!!! –charlie

Avatar image for Dan Bader

Dan Bader RP Team on May 29, 2019

@charliem22: Awesome, glad to hear it!! :)

Avatar image for Koert

Koert on June 23, 2019

How difficult can it be to work with virtual environments? Never got it but thanks to your videos I finally understood it! Thanks again.

Avatar image for Abby Jones

Abby Jones on June 27, 2019

This is awesome.

Avatar image for DiscreteLoner

DiscreteLoner on Aug. 8, 2019

Easy to follow up.

Avatar image for Rob Black

Rob Black on Aug. 13, 2019

Excellent micro course on virtual environments - exactly the size and pace that I needed. I viewed this course as a side-bar to the tutorial I’m reading now: realpython.com/flask-connexion-rest-api/. Thanks!

Avatar image for UBBA

UBBA on Aug. 27, 2019

Great video. I am working in VSCode in Windows 10. Couple things I noticed:

  • Which equals where in windows.
  • Did not know that tree worked in windows command prompt
  • VSCode will find your virtual environment and activate it when chosen.
  • You can pip install right from the VSCode terminal for the venv.
Avatar image for Ravi

Ravi on Dec. 6, 2019

Hi Dan,

In the supporting material, you have mentioned about pyenv, what I understood is we can either use virtualenv or pyenv to maintain dependancies and need not use both of them. Is my understanding correct?

Avatar image for Dan Bader

Dan Bader RP Team on Dec. 6, 2019

@Ravi: I actually use a combination of pyenv and venv in my personal workflow.

I use pyenv to install different versions of the Python interpreter on my machine that I might need. So let’s say I have Python 3.7.3 installed, and I also want 3.8.0 for another project. I’ll install these Python versions with pyenv.

And then each individual project still gets a local virtual environment via venv so I can manage installed packages independently.

You can find more info on pyenv in this tutorial: realpython.com/intro-to-pyenv/

Avatar image for Rory

Rory on Dec. 11, 2019

Dan, thanks to you and your team for these tutorials. #1 Takeaway? Being able to segregate my code. Still in the learning stage and all my exercise code is in a “Scripts” folder. This will be nice when I start focusing on larger projects. So much to learn! So little time! Thanks again!

Avatar image for Anonymous

Anonymous on March 22, 2020

How do you best deploy a virtual environment from a Linux dev environment to a production server environment? Is there any special set up, or can you just move the entire directory over and it runs without issues?

Avatar image for Dan Bader

Dan Bader RP Team on March 22, 2020

Copying the directory over might work if you’re using the exact same operating system and Python setup in your dev environment and on your production server. I wouldn’t recommend it though.

Typically what people do is set up the Python environment + install dependencies from your deploy script, or use something like Docker to build a containerized version of the app that can be used for local testing, staging, and in production.

If you’re using a platform-as-a-service (PaaS) provider like Heroku, they usually come with their own way of configuring the version of the Python interpreter and will install packages from a requirements.txt file upon deploy.

realpython.com is hosted on Heroku and I find that it’s quite a convenient workflow.

Avatar image for Brandon Austin

Brandon Austin on March 30, 2020

Great and quick to the punch course. Good stuff Dan - really loving the content.

Avatar image for John B

John B on April 16, 2020

Thanks Dan! Concise and clear. Just what I needed.

Avatar image for Alan ODannel

Alan ODannel on May 6, 2020

I’ve used the Python virtual environments, it’s been a while and I wanted a refresher. This was just what I needed.

Avatar image for James Butler

James Butler on May 21, 2020

Good tutorial but I encountered a couple of differences between mac and windows 10. First, on windows my command python is 3.7.7 so, I don’t have to use python3. Second, when I executed the command python -m venv ./venv, I get this dir structure; include Lib Scripts No bin dir. I also had to use the git bash terminal in windows 10 because windows does not have terminal commands like source, and which. I was able to activate and deactivate in git bash terminal. Thanks

Avatar image for Ricky White

Ricky White RP Team on May 22, 2020

Hi James. On windows you can activate your venv without using git bash with:

// If using CMD

// If using Powershell

You can deactivate the environment by just typing deactivate. Hope that helps.

Avatar image for aboahmedah

aboahmedah on May 28, 2020

Hi. Great course. How do i setup the standard python IDLE editor to use the interpreter and environment that i need for different projects.

Avatar image for Mark

Mark on June 6, 2020

Thanks Dan, Great stuff

Avatar image for stefhx

stefhx on June 15, 2020

Nice one!

Avatar image for Srini

Srini on June 25, 2020

Thanks, Dan. It is a nice session.

Avatar image for koinonia9912

koinonia9912 on July 20, 2020

I used ‘where pip3’ on command prompt in Windows 10,as follows: ‘’’ (base) C:\Users\Craig\Miniconda3\envs>where pip3 C:\Users\Craig\Miniconda3\Scripts\pip3.exe C:\cygwin64\bin\pip3

(base) C:\Users\Craig\Miniconda3\envs> ‘’’ Which pip3.exe gets used?

Avatar image for Yvonne Wilmot

Yvonne Wilmot on Sept. 7, 2020

Thanks that was very clear and helpful. Now I need to do this in a Windows environment. Picked up some pointers from the previous comments so those are helpful too.

Avatar image for Doug Ouverson

Doug Ouverson on Oct. 8, 2020

Thanks Dan for clear and concise explanation of virtual environments.

One thing that wasn’t covered was how to set the Python version when creating a project.

I see in a comment above that “I actually use a combination of pyenv and venv in my personal workflow.”

I’m not understanding how these work together. Would you please referr me to course and/or resoucre?

Thank you.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Oct. 9, 2020

@Doug Ouverson Managing Multiple Python Versions With pyenv might be helpful.

Avatar image for Ghani

Ghani on Oct. 27, 2020

Very useful course; thanks!

Avatar image for gregorysaison

gregorysaison on Nov. 12, 2020

A little bit difficult to adapt the instruction for a configuration Powershell on Windows 10 but with a little bit of research it was not impossible to passing through.

It’s really a good introduction to virtual environment for a person like me who has no knowledge on this subject.

Thanks !

Avatar image for thecocoa

thecocoa on Dec. 7, 2020

Dan, I appreciate the quick tutorial. As I’m on the road to becoming a pythonista, I see a lot of suggestions across the interwebs like: “You should always use a virtual environment when programming in Python.” Does this apply as I’m beginning to learn Python - or - only once I actually begin creating projects?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 7, 2020

@thecocoa A virtual environment is a fresh copy of the Python interpreter, which lets you avoid library version conflicts. It makes sense to create one if you intend on installing some external dependencies that aren’t available out of the box in the standard library. As long as you don’t need anything extra, which is likely the case for beginners, you can get away without using virtual environments. On the other hand, mastering virtual environments early on will make things easier for you in real projects, which often benefit from them.

Avatar image for voipguy

voipguy on Dec. 12, 2020

Thanks, It’s beginning to “sink in” & on my way to madness.

an OLD mechanical man :-)

Avatar image for mdoudiarra

mdoudiarra on Jan. 14, 2021

Thank you for this tutorial, great job :) I have a questions please, I use your tutorial to install a virtual environement, with miniconda3, because I want to work with Gdal. But when I install gdal and import them, I get an Error, and the shell show me the path for my older install on python2. How can I isolate my virtual env with python3?

Thank you

Avatar image for Ricky White

Ricky White RP Team on Jan. 15, 2021

That’s odd, as it should default to Python3 with miniconda3. You can also specify the python version when you create a virtual env with conda:

conda create -n myenv python=3

Hope that helps.

Avatar image for qmark42

qmark42 on April 17, 2021

Hello. I’m just starting to get the feel for more than just simple enter information to the interpreter and store it or out put it, using no more than 100 lines per program. I have found recently that i need to know more (much) and delve into gui’s as well as other more complex “stuff”. This lesson and a couple of others that I started and finished have provided me with background and information continue my quest.

Avatar image for Jesus Espinoza

Jesus Espinoza on July 5, 2021

Great tutorial!! Thanks for sharing!!

Avatar image for Philip GeLinas

Philip GeLinas on Sept. 25, 2021

Short, sweet, to the point. Excellent!

Avatar image for aniketbarphe

aniketbarphe on Nov. 25, 2021

Excellent Session! Thank You!

Avatar image for mwolf

mwolf on Jan. 9, 2022

Thank you very much! Very well explained and presented!

Avatar image for L Kenneth Wells

L Kenneth Wells on Dec. 27, 2022

I’ve always wanted t understand this and now its super simple. Thank you. Also I was wondering should I learn Linux if I run everything on Windows? Just curious and thank you!

Avatar image for atenn99

atenn99 on Jan. 2, 2023

Just rejoined RealPython and this has been super helpful in making me realise what venvs are & why they are super important. Excellent tutorial. Off to create my new venv!

Avatar image for Danadasa Chan

Danadasa Chan on July 26, 2023

Hi Dan. Thanks for this very informative and easy to understand tutorial. When I attempted to activate my virtual environment, I got an error message that said I could not run a batch file because execution policy default setting is Restricted. To fix this, I opened a powershell prompt with administrative privileges and I set execution policy to RemoteSigned. And how the batch file executes and everything works just fine. I am wondering if I should change execution policy back to Restricted after I am done coding, or can I leave as RemotedSigned.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 7, 2023

@Danadasa Chan If you’re going to be working in virtual environments frequently, then it’s probably more convenient to leave the execution policy as it. I’m not a Windows expert myself, but I think that you should be able to narrow down the scope of this policy to only apply to a particular user account or script. Maybe you can find more details in our Windows Setup Guide?

Avatar image for SirVeyor

SirVeyor on April 6, 2024

What are the best practices for venv? Should you create virtual environments for each project? Can you use the same venv for multiple projects that are using the same python version?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on April 8, 2024

@SirVeyor It’s considered the best practice to create a separate virtual environment for each of your projects so that their dependencies remain isolated from each other. Technically, you can reuse the same virtual environment for multiple projects by activating it before running the corresponding source code. That may sometimes make sense, but in general, you want to have a virtual environment per project, which is why you often keep it in a local .venv/ folder inside of your project.

(By the way, a virtual environment is a lightweight copy of a particular Python interpreter, which means that it’s tied to that specific Python version.)

Become a Member to join the conversation.