1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Third-Party Package Management in Python

Managing Packages for Multiple Projects

Start course
Difficulty
Beginner
Duration
51m
Students
9
Description

This content is designed to introduce new developers to managing third-party packages using the Python package index and a tool called Pip. This course is part of a series of content designed to help you learn to program with the Python programming language. 

Learning Objectives

  • The Python Package Index
  • The tool used to manage packages called pip
  • The tool used to create isolated Python environments named venv
  • Considerations to be made before installing a third-party package

Intended Audience

This course was designed for new developers wanting to learn Python. 

Prerequisites

  • That you’re familiar with the Python runtime
  • That you’re familiar with the Python language syntax
  • This course assumes that you’re not yet familiar with how to install and manage third-party Python software
Transcript

The Python runtime expects third-party packages to be installed in the site-packages directory. Due to the distribution package structure we can only use one version of a package at a time. Which means if we have two applications with conflicting requirements we can run into a problem. 

In this lesson we’re going to review how to use the built-in venv module to create isolated Python application environments. 

The need to create isolated environments for each application is so common that it’s built into the standard library. Built into modern versions of Python is a module named venv which stands for virtual environment. 

We’re not going to perform a venv deep dive in this course. However, because it’s such a valuable module to use, I want to demo it in this course, in the context of package installation. The venv module can be called as a command line application using Python’s minus m flag and specifying venv. 

We also need to specify the name of the directory where our virtual Python environment will reside. It’s a common practice to name them dot-venv due to the fact that files starting with a period are hidden from text editors and file system viewers - on certain operating systems.

Let’s try this out. I’m going to name this virtual environment env_one. Notice that after it completes I now have a new directory named env_one. 

The venv module creates a new set of directories for the Python runtime. It copies and or links to the required files from the version of Python we’re currently using to call the venv module. As a part of this directory structure is a site-packages directory. This environment is an isolated Python runtime. 

There are a couple ways to use this environment. First, it’s possible to specify this exact python binary on the command line. Calling this exact version of Python will start the runtime with all of the files in our virtual environment.

There’s another solution that attempts to make it a little easier to run this new instance of Python. 

Inside the directories in the virtual environment are some scripts that when run will perform some configuration changes to the current shell session. One of these changes results in us being able to simply type python or python3 and have the shell know to use the virtual environment’s python binary.

When using the helper scripts to configure your shell session you can call the activate script to perform the configuration and the deactivate script to revert the changes. The exact way to activate and deactivate a virtual environment depends on the operating system. 

When activated we can use pip to install packages just for this environment. Then we can run code using this environment’s version of Python and the code will have access to the installed packages. By using virtual Python environments for different applications you’ll be able to better manage external packages. 

Alright, we’re going to wrap this lesson up here. There’s a lot venv has to offer however, for just understanding the problem it solves is sufficient. 

Here are your key takeaways for this lesson:

  • The purpose of the site-packages directory is to store third-party packages for the Python runtime to use.

  • The purpose of venv is to create and manage isolated Python environments.

    • Venv creates a new directory containing the files required by the Python runtime. 

    • Including the site-packages directory. 

  • Virtual environments provide scripts to activate and deactivate the environments.

    • Making it possible to run Python without needing to specify the full path of the Python binary.

Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!

 

About the Author
Students
93998
Labs
28
Courses
45
Learning Paths
54

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics