Practical Guide to Python

Virtual Environments

Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Virtual Environments" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina shares best practices around using virtual environments. A requirements.txt file is used to annotate dependencies for a virtual environment. This file can be created manually or generated automatically by Python's package manager.


Transcript from the "Virtual Environments" Lesson

>> Now we briefly touched about virtual environments. They are an important way of storing the dependencies that your Python application requires. Creating that virtual environment will make a self-contained folder that includes that full Python installation. And it's going to allow you to install dependencies that are specific to that application.

And a specific version of Python without the possibility of messing up your global version of Python might not be the same version. You're not gonna have the same libraries that you're using between different projects, which is why isolating in a virtual environment is very important. You might see this shortened as virtual and for short, a standard that you might see is a requirements.txt file that's included with a Python project.

It's a special file that we're gonna use to tell pip just Python's package manager command line tool which dependencies to install for this particular package. The format is simple, you just put each dependency manually on one line in this file. If you need to specify a specific version, you can do double equals and put the version after the name of the library.

Or you can just install each dependency in your virtual environment using pip directly. Once you've done that, probably as you're building the application, you can see which packages are present in your virtual environment with a command called pip freeze. You can pipe that into a requirements.txt file to generate that for you automatically.

Unfortunately, that also includes a lot of the sub dependencies. So I prefer if I'm working on a simple project to manage these requirements.txt files manually, but that's not important. If you get a project with a requirements.txt file, you can do a pip install-r in your virtual environment to install all of the dependencies specified in this file.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now