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

Unlock This Lesson

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

Unlock This Lesson

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.

Recap and Summary

Give Feedback
  • Virtual environments keep your project dependencies isolated.
  • They help you avoid version conflicts between packages and different versions of the Python runtime.
  • As a best practice, all of your Python projects should use virtual environments to store their dependencies.

aravind on Sept. 17, 2019

hi, if we are packaging the python py files/project as well as the python run time binaries in a docker container, then i assume we dont need to worry about venv because the container will use a immutable deployment?

Geir Arne Hjelle RP Team on Sept. 17, 2019

I usually do not use a virtual env inside of Docker. However, there are a few scenarios where it might still be useful to do so. This SO answer, and links within it highlights some of them: stackoverflow.com/a/29359760

  • If your container depends on some Python based OS-tools, you might still want to isolate your code from the container system Python (although I believe the Python images on DockerHub already do this
  • If you do multistage builds pulling out just the production code, it might help having everything isolated in a virtual environment. This would be more common and more powerful with compiled code where you have many build tools, though.

Finally, if you actually use virtual environments inside Docker, there are a few traps to be aware of. In particular, each command in a Dockerfile is run as a separate process, so special care must be taken to keep the environment active. This blog post has some good solutions: pythonspeed.com/articles/activate-virtualenv-dockerfile/

Zarata on Oct. 19, 2020

My motivation in finally taking this (excellent) course was that I recall somewhere (I think another RealPython course) it was stated one does not usually wish to globally upgrade Python on their Linux install, which generally utilizes a given Python version already. I’m wishing to upgrade to v3.9 and it appears Ubuntu (much less CentOS) haven’t caught up yet. I suspect the actual “how to” make an upgrade in a venv is covered in that half-remembered or another RealPython “somewhere”. A link to such a tutorial, or inclusion of a special lesson, from within this course might be nice :) [True, the RP search is very excellent … I’m just lazy]

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

@Zarata You might be talking about Managing Multiple Python Versions With pyenv tutorial.

Become a Member to join the conversation.