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

Recap and Summary

  • 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.
Avatar image for aravind

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?

Avatar image for Geir Arne Hjelle

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/

Avatar image for Zarata

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]

Avatar image for Bartosz Zaczyński

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

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

Avatar image for alexinedmonton

alexinedmonton on Jan. 18, 2021

I’m not sure I understand using virtual environments with IDEs or Jupyter Notebooks (I’m using Windows). Do I need to open a command prompt and keep that open while I work in an IDE or Notebook? How do the Notebooks know which environment to use?

Thanks

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 19, 2021

@alexinedmonton To manage virtual environments, you typically use specific commands in the terminal. However, that will only affect your current terminal session until you close it. If you open a code editor by clicking an icon on the desktop, it won’t know that you changed the virtual environment elsewhere.

Different IDEs bundle their specific means for managing those environments. If you’re talking about PyCharm, for instance, then you’ll find a drop-down in the bottom right corner of the main window. You can also choose a virtual environment in project settings.

For Jupyter Notebooks, things get a little more complicated because you have to install a new “kernel” for every environment you want to work with. I’ll refer you to resources online for more details. Here’s one that might help you: janakiev.com/blog/jupyter-virtual-envs/

Avatar image for mikecastillouvu

mikecastillouvu on March 25, 2022

So, he mentions how to use a specific version of Python in that venv. how do we implement a specific version of python in that venv? better yet, how do we keep our IDE (such as VS Code) from using the correct version of python in that venv?

Thanks in advance

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on March 27, 2022

@mikecastillouvu Check out our Managing Multiple Python Versions With pyenv tutorial for more details on that.

Become a Member to join the conversation.