Managing Python Dependencies Transcripts
Chapter: Managing Third-Party Dependencies With pip
Lecture: Installing Packages With pip

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Next, you'll learn how to install packages with pip. We're going to cover the different ways you can install packages from PyPi,
0:09 how you can install packages from other sources, like git and other version control mechanisms,
0:16 how you can update packages and how you can uninstall them again. This is how you install packages with the pip command line tool.
0:24 Basically, you open a terminal, and you run the pip install command, passing it the name of the package that you want to install.
0:32 And by default, this is going to install packages from the PyPi package repository. Pip also caches packages locally, to speed up repeated installs.
0:42 Let's take a look at a quick demo here. I am in a new terminal session here, and now I am going to walk you
0:48 through how to install a Python package using the pip install command line. So, the pip command line interface has a couple of useful commands,
0:57 for example, there is the pip list command, that I can use to inspect which packages are installed at the moment, so let's run this.
1:04 So this lists all of the packages that are installed into my global Python environment right now, this is a really base line setup
1:11 and it doesn't really include anything besides pip and its dependencies, what I am going to do now is install a third party package,
1:19 and for that we're going to use the Requests module. So when you look at this list, you can see that it definitely
1:25 does not include Requests right now, so let's change that. So this is the pip install command to install the Requests library, let's run it now.
1:34 Alright, so the installation completed successfully, and now when I run the pip list command again, Request should show up in that list.
1:46 Yeah, here it is, we successfully installed Requests. Let's try it out, so I am going to jump into a Python interpreter session now,
1:52 and I am going to try and import the Requests module that we just installed, alright, this succeeded, yeah, that looks pretty good,
2:06 let's try and actually fire off a real HTTP request. Perfect, that worked just fine.
2:24 Another handy command is pip show, you can use it to display metadata and information about locally installed packages,
2:32 so I am going to go ahead and run that on Requests now. So you can see here, for example, the name and the version
2:40 of the library that was installed, a quick summary, the homepage for the library, the license and also where it was installed locally on my machine.
2:48 So you just saw how to install a package from PyPi using the pip install command,
2:53 now what this will do is it will always install the latest version of the package that is currently available, so what do you do if you want to install
3:01 an older, or a specific version of a package instead? Pip has you covered there. You can actually pass so called version specifiers to package names
3:10 when you install them through pip install, with this pip install requests==2.1.3 command, I am installing a specific version of Requests
3:21 and these version specifiers can get pretty flexible, for example, I can do this and actually pass a version range for this Requests module,
3:30 so in this case, I would tell pip to install a version of Requests that is between version 2.0 and 3.0, so essentially,
3:39 I would tell pip to install the latest version in the version 2 series of Requests.
3:45 One more feature when it comes to version specifiers, is the ~=specifier.
3:51 This tells pip to install a version of Requests that is compatible with version 2.1.3
3:57 in practical terms, that means it's going to try and install a version of the 2.1 branch of Requests, that is going to be at least version 2.1.3,
4:11 so 2.1.4 and 2.1.5 and so on, would all be candidates for this. Now this is handy for example if you want to get minor version updates automatically,
4:21 but you still want to retain some control over larger upgrades, if you're using version specifiers, I would generally recommend
4:29 that you are very specific with them because this can help avoid surprises.
4:33 Later on in this course, we're going to come back to these version specifiers, and you're going to learn how to use them to set up fully reproducible
4:40 Python environments for your applications. Before you move on, here is a quick warning about installing packages globally using pip.
4:49 When you use the pip install command, by default, it will install Python packages into the global Python environment.
4:56 This means that any package you install this way is going to be shared across the whole system or across your whole user account,
5:04 now this might be completely okay, if it's done intentionally, for example, in order to install Python based command line tools,
5:12 like httpy in this case, you probably want to make sure that you can access the tool from anywhere in the system.
5:20 Most of the time you should prefer so called virtual environments, they are a way to keep your Python packages nice and separate by project,
5:28 and that way you can make sure you're not cluttering up the global environment, later on in this course, you are going to learn
5:34 how to set up and use virtual environments, this will help you avoid version conflicts and we'll make sure that each project you're working on
5:43 has its own Python environment, to install packages into. Here is another useful feature for installing packages with pip.
5:51 You just learned how you can install packages from a package repository like PyPi, but pip also supports other sources, for example,
6:01 you can install packages directly from git and other version control systems,
6:05 here is what this would look like for installing a package that is hosted on GitHub,
6:10 you would pass a url pointing to a git repository to the pip install command. And you can even control which branch
6:18 or a specific commit in that repository you want to install. Here are a few examples that install the Requests library directly from GitHub.
6:27 In the first example, I am putting the at master specifier, to install the latest version of Requests directly from its master branch on git.
6:38 In the second example, I am using a commit hash specifier to install a very specific version of the Requests library.
6:46 In the third example, I am pointing pip at a specific git tag to install Requests from. You can see here how this gives you a lot of flexibility,
6:56 you might use this functionality to install a very specific versions of a library,
7:01 for example, if you're waiting for a bug fix to land in the official release,
7:06 and you really can't afford to wait so sometimes maybe you want to install the latest version of a library, directly from its source repository.
7:14 or, you could use this feature to install private packages and libraries that are not available on PyPi. So you would just host your own library
7:25 in a git repository somewhere, and then point pip towards that. generally though, if you can install a package
7:32 from a proper package repository like PyPi, then I would definitely do it. Installing a Python package directly from
7:40 a version control system is a bit of a special case. Typically, you wouldn't use that functionality to install any old publicly available package,
7:50 but you would reserve it for those special moments when you really need to be on the cutting edge and maybe need to install a specific commit
7:57 of a package to get your program to work. This is definitely useful but I would also handle it with care, and stick to PyPi when possible.


Talk Python's Mastodon Michael Kennedy's Mastodon