Installing Packages With Pip
00:00 Next, you’ll learn how to install packages with pip. We’re going to cover the different ways you can install packages from PyPI, how you can install packages from other sources, like Git and other version control mechanisms, how you can update packages and how you can uninstall them again.
This is how you install packages with the
pip command line tool. Basically, you open a terminal, and you run the
pip install command, passing it the name of the package that you want to install.
So, the pip command line interface has a couple of useful commands, 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.
01:03 So this lists all of the packages that are installed into my global Python environment right now, this is a really base line setup 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, and for that we’re going to use the Requests module.
So when you look at this list, you can see that it definitely 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.
01:48 Let’s try it out, so I am going to jump into a Python interpreter session now, and I am going to try and import the Requests module that we just installed, alright, this succeeded, yeah, that looks pretty good, let’s try and actually fire off a real HTTP request.
Perfect, that worked just fine. Another handy command is
pip show, you can use it to display metadata and information about locally installed packages, so I am going to go ahead and run that on Requests now.
02:35 So you can see here, for example, the name and the version 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.
So you just saw how to install a package from PyPI using the
pip install command, 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 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 when you install them through
pip install, with this
pip install requests==2.1.3 command, I am installing a specific version of Requests and these version specifiers can get pretty flexible, for example, I can do this and actually pass a version range for this Requests module, 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, I would tell pip to install the latest version in the version 2 series of Requests.
03:56 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, so 2.1.4 and 2.1.5 and so on, would all be candidates for this.
04:16 Now this is handy for example if you want to get minor version updates automatically, but you still want to retain some control over larger upgrades, if you’re using version specifiers, I would generally recommend that you are very specific with them because this can help avoid surprises.
Before you move on, here is a quick warning about installing packages globally using pip. When you use the
pip install command, by default, it will install Python packages into the global Python environment.
04:55 This means that any package you install this way is going to be shared across the whole system or across your whole user account, now this might be completely okay, if it’s done intentionally, for example, in order to install Python-based command line tools, like HTTPie in this case, you probably want to make sure that you can access the tool from anywhere in the system.
05:19 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, 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 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 has its own Python environment, to install packages into.
Here is another useful feature for installing packages with pip. You just learned how you can install packages from a package repository like PyPI, but pip also supports other sources, for example, you can install packages directly from Git and other version control systems, here is what this would look like for installing a package that is hosted on GitHub, you would pass a URL pointing to a Git repository to the
pip install command.
06:37 In the second example, I am using a commit hash specifier to install a very specific version of the Requests library. In the third example, I am pointing pip at a specific Git tag to install Requests from.
06:52 You can see here how this gives you a lot of flexibility, you might use this functionality to install a very specific versions of a library, for example, if you’re waiting for a bug fix to land in the official release, 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.
07:13 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 in a Git repository somewhere, and then point pip towards that.
07:29 Generally though, if you can install a package from a proper package repository like PyPI, then I would definitely do it. Installing a Python package directly from a version control system is a bit of a special case.
07:42 Typically, you wouldn’t use that functionality to install any old publicly available package, 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 of a package to get your program to work.
Become a Member to join the conversation.