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

Installing Packages

00:00 In this lesson, you’ll perform some typical actions that you’ll do when working on a Python project. You’ll activate your virtual environment, install an external package, and list installed packages. Along the way, you’ll learn a bit more about project dependencies.

00:15 So this lesson brings the content of the former lessons together. You’ll use the terminal, a virtual environment, and pip. That’s exciting, right?

00:23 So let’s hop into it.

00:26 Here I am in the project folder. Notice that the prompt has no prefixed environment name in parentheses. That means I currently don’t have any virtual environment activated.

00:38 Before activating the virtual environment, let’s check something first. When you run python3 -m pip list, you can list the currently installed packages.

00:50 Here you can see that my system’s Python has pip and setuptools installed as packages that were grabbed via pip. For you, there could be a different packages listed.

01:02 The important thing here is that these are the packages available for my system’s Python. We’ll come back to this in a bit. Now, let’s activate a virtual environment with source venv/bin/ activate, and now there is venv in parentheses at the beginning.

01:20 That means the virtual environment is activated. When I run the pip list command again, then you can see this. There is no real difference in the listed packages yet.

01:33 So let’s change that.

01:37 To install an external package into a virtual environment, you must activate a virtual environment first. Then you use the pip install command with the package name.

01:48 You used the pip install command before, but with the --upgrade option. So to just install a package that isn’t installed yet, you type python3 -m pip install, and then the package name.

02:04 As an example, you’ll install the requests package. The requests package is used for making HTTP requests from a Python program. It’s extremely useful in a variety of domains, and many projects use it, but we won’t use it in this course.

02:19 It’s just an example for installing an external package.

02:26 In your terminal, you type the following: python3 -m pip install, and then the package name, which is requests. While pip is installing the requests package, you’ll see a bunch of output.

02:41 Notice that pip first tells you that it is collecting requests. You see the specific filename that pip is downloading and the progress bar.

02:50 After that, you see that pip installs four more packages: idna, charset_normalizer, certifi, and urllib3.

03:01 These packages are dependencies of the requests package. That means that requests requires these packages to be installed for it to work properly.

03:12 You can verify that these were requirements of requests by typing python3 -m pip show requests.

03:26 The pip show command displays information about an installed package, including the author’s name and an email and an homepage. It also lists that requests requires certifi, charset-normalizer, idna, and urllib3.

03:44 Once pip is done installing requests and its dependencies, you can run pip list in your terminal again. Now pip list shows the packages that you installed as well.

03:59 So when you install a package, more often than not, you also install some packages with it. That’s an important detail about installing packages with pip.

04:10 These other packages are called dependencies or requirements. If you wouldn’t use a virtual environment for your projects, then your system’s Python packages would clutter quite fast with all these dependencies.

04:23 And this can be especially problematic, as different packages require different versions of their dependencies. So with a virtual environment, you minimize this problem tremendously.

04:37 To wrap this lesson up, deactivate your virtual environment and run the pip list command again. So let’s type deactivate, Enter, python3 -m pip list.

04:50 And as you can see, your system’s Python package list stayed clean and tidy. All right, now that you know how to install packages, I’ll see you in the next lesson, where you’ll learn how to uninstall packages.

Avatar image for Steve S

Steve S on Aug. 19, 2023

Hi, was going through this course and was wondering if using the command ‘python3 -m pip install <package>’ was/does anything different than entering ‘pip install <package>’?

Is it best practices to use the first command or personal preference?

Thank you.

Avatar image for Bartosz Zaczyński

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

@Steve S Indeed, using the more explicit python3 -m pip install command is considered the best practice because it prevents you from installing packages to the wrong Python interpreter. Brett Cannon wrote a blog post some time ago, explaining the arguments for it: Why you should use python -m pip .

Become a Member to join the conversation.