A good knowledge of how to use the
pip command to manage Python packages can make the difference between a jumbled mess of spaghetti code and a nicely organized codebase. This guide will teach you the basics of using pip effectively so you can spend more time writing, and less time wrangling code.
What is pip
pip is a package manager for the Python programming language. It provides access to a massive repository of open source tools and libraries, as well as the ability to manage your own proprietary packages. One of the greatest benefits to using a package manager is that it automatically installs the dependencies of any package you install, allowing you to more quickly get back to writing some code.
The good news is that pip is built into Python versions 2.7.9 or later in the 2.X series and 3.4 or later in the 3.X series. If you are using an older version of Python, you’ll need to upgrade to get pip natively. However, if that is not possible, it is easy to install by downloading the bootstrap script and running it from the command line like so:
Even if you have a modern version of Python with pip prepackaged, it may be out of date by the time you read the guide. Fortunately, it has the wonderfully useful ability to upgrade itself, like so:
pip install -U pip
or on Windows:
python -m pip install -U pip
The different upgrade command in Windows is necessary because in Windows it is not possible to overwrite a running executable. With this upgrade command, the running executable is actually
python.exe, which makes
pip.exe available to overwrite.
Now that you have pip, it’s time to start managing some packages! Simply enter
pip into your shell to get a list of pip commands and an overview of the options available to you. Let’s walk through their usage.
search command allows you to search through the database of packages in Python’s central package repository, PyPI. For example:
pip search django
This search will generate a list of the over 8,000 packages with the term “django” in either the name or the description of the package. The lookup is case-insensitive, so this search will return results matching “django” or “DJANGO” (or any other case variation, such as “Django” or even “djAngo”).
Arguably the most useful command provided by pip, the
install command installs a Python package and its dependencies into your Python environment. The simplest usage is similar to
search; just specify the name of the package you wish to install.
pip install Flask
install command is also case-insensitive. You can also install a specific version of a package or a range of versions. One reason you might want to specify a range is if you require a package as a dependency to your own code, and you wish to indicate that your code will work with any version in that range.
pip install Flask==0.10.1
# install this particular version
pip install "Flask>=0.9,<0.11"
# install versions matching the range from 0.9 up to (but not including) 0.11
You can also put a list of packages (one per line) in a text file and install all of them at once using the
pip install -r requirements.txt
install command can also upgrade packages (as illustrated above in Upgrading pip) by including the
--upgrade option. Despite the
--upgrade name, the option can be used anytime you need to change the version of an installed package, whether the new version is higher or lower than the installed version.
If the package you wish to install is not in the PyPI repository, then you can install from a version control system. For example:
pip install git+https://github.com/cherrypy/cherrypy.git
Of course, sometimes you need to remove packages. The
uninstall command removes the specified package(s) from your environment.
pip uninstall Pyramid
If building a list of all the packages you need sounds like a lot of work, then the
freeze command is for you. This command outputs all the currently installed python packages and their versions. You can also redirect this output to a text file to build a ready-made source to use with the
pip freeze > requirements.txt
We have barely scratched the surface of the feats you can accomplish with pip in this tutorial. The best way to learn more is to get out there and use pip to manage your Python project’s dependencies. For reference, you can find the complete pip documentation here.
Thank you for following along this how-to, we hoped you have enjoyed it. Please check back here for more updates or check out the many services offered by Atlantic.net, including HIPAA Compliant Cloud Storage Hosting, Web Server Hosting and SQL Hosting.