The ecosystem of Python with all the helpful third-party libraries makes it such a productive language. It is therefore time to look where we can find them and how we can install those packages. For this post I use the term packages to describe the distribution units of Python code, not the way how you can organise your code using folders and the __init__.py
file. That will be the topic of a later post.
This post is part of my journey to learn Python. You can find the other parts of this series here.
PyPI – the Python Package Index
Until now all the things I explained in the Python Friday series used the built-in packages. Like all other programming languages, Python has already a lot of things built-in that you can use to create your application. However, we do not need to reinvent the wheel every time we have to solve a problem. Often there is already a solution that we can reuse. For Python you find the packages on PyPI.org.
At the time of writing this post you can find more than 224’000 projects on PyPI. While the search function is a great help, you may still not find what you are looking for. A good help to find packages for a specific use case is to check the curated list of Awesome Python. If you want to do something with email, you click on the email entry and only need to check the 6 libraries it suggests:
Pip – the Python Package Installer
You can use the tool pip for all things related to packages. You can install packages, remove them or get the newest version. Before we can do anything, we need to install pip itself. If you have a current version of Python installed on your system, then you most likely have pip as well. Run this command in the Command Prompt (cmd) to check if this the case:
1 2 |
pip -V pip 19.3.1 from c:\****\lib\site-packages\pip (python 3.8) |
If this command results in an error, then go to http://pip-installer.org/ and follow the instructions to install pip.
WARNING
The commands in the rest of this post will modify your system. If you know what you do, then everything is OK. However, in most cases you want to install packages for a specific project and not for your whole system. The next post will be about virtual environments that solve exactly this problem.
Getting a list of installed packages
To see all installed packages on your environment, you can run pip list:
1 2 3 4 5 6 7 8 9 |
pip list Package Version ------------------ ------- bleach 3.1.0 ipykernel 5.1.3 ipython 7.11.1 ipython-genutils 0.2.0 jedi 0.15.2 Jinja2 2.10.3 |
Install a package
To install the newest version of a package, you can use the install command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
pip install requests Collecting requests Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 899 kB/s Collecting chardet<4,>=3.0.2 Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) |████████████████████████████████| 133 kB 2.2 MB/s Collecting idna<3,>=2.5 Downloading idna-2.9-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 2.7 MB/s Collecting certifi>=2017.4.17 Downloading certifi-2019.11.28-py2.py3-none-any.whl (156 kB) |████████████████████████████████| 156 kB 6.4 MB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB) |████████████████████████████████| 125 kB ... Installing collected packages: chardet, idna, certifi, urllib3, requests Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.8 |
The install command not only installs the package you specify, but also all its dependencies.
Sometimes you need a specific version of a package. You can submit the version you need directly after the package name:
1 2 3 4 |
pip install requests==2.20.0 Collecting requests==2.20.0 Downloading requests-2.20.0-py2.py3-none-any.whl (60 kB) ... |
Remove a package
You can remove an installed package using the uninstall command:
1 2 3 4 5 6 7 8 |
pip uninstall requests Found existing installation: requests 2.23.0 Uninstalling requests-2.23.0: Would remove: c:\users\jgraber\appdata\local\programs\python\python38-32\lib\site-packages\requests-2.23.0.dist-info\* c:\users\jgraber\appdata\local\programs\python\python38-32\lib\site-packages\requests\* Proceed (y/n)? y Successfully uninstalled requests-2.23.0 |
The uninstall command does only uninstall the package you specify, not its dependencies. If you want to clean-up you need to do this on your own.
Upgrade a package
You can upgrade a package using the –upgrade option for the install command. Before you run this command you should read the documentation and check if the upgrade strategy of pip is what you need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
pip install --upgrade requests Collecting requests Using cached requests-2.23.0-py2.py3-none-any.whl (58 kB) Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in c:\users\jgraber\appdata\local\programs\python\pyt hon38-32\lib\site-packages (from requests) (3.0.4) Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in c:\users\jgraber\appdata\local\programs\python\py thon38-32\lib\site-packages (from requests) (2019.11.28) Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in c:\users\jgraber\appdata\local\programs\python\python38 -32\lib\site-packages (from requests) (2.7) Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\jgraber\appdata\loc al\programs\python\python38-32\lib\site-packages (from requests) (1.24.3) Installing collected packages: requests Attempting uninstall: requests Found existing installation: requests 2.20.0 Uninstalling requests-2.20.0: Successfully uninstalled requests-2.20.0 Successfully installed requests-2.23.0 |
You can use the same command to upgrade pip itself. When a new version of pip is available, you will get a warning like this one at the end of the output of any pip command:
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the ‘python -m pip install –upgrade pip’ command.
List outdated packages
Finding outdated packages is in many programming languages a challenge. In Python you can simply use pip list -o to get this list:
1 2 3 4 5 6 7 8 9 10 |
pip list -o Package Version Latest Type --------------- ------- ------ ----- bleach 3.1.0 3.1.3 wheel decorator 4.4.1 4.4.2 wheel ipykernel 5.1.3 5.2.0 wheel ipython 7.11.1 7.13.0 wheel jedi 0.15.2 0.16.0 wheel Jinja2 2.10.3 2.11.1 wheel jupyter-client 5.3.4 6.1.0 wheel |
Get information about installed packages
You can use the show option to get the basic information about a package. Next to the version number you most likely want to know the web site or the location of this package on your machine:
1 2 3 4 5 6 7 8 9 10 11 |
pip show jedi Name: jedi Version: 0.15.2 Summary: An autocompletion tool for Python that can be used for text editors. Home-page: https://github.com/davidhalter/jedi Author: David Halter Author-email: davidhalter88@gmail.com License: MIT Location: c:\users\jgraber\appdata\local\programs\python\python38-32\lib\site-packages Requires: parso Required-by: ipython |
Next
There is an incredible number of packages ready to use with your applications. You can find them in the PyPI and install them with pip. However, before you know it your system is filled with packages you no longer need, and everything looks like a mess. The next post will show you a way to prevent your system to end up as a package dump.
2 thoughts on “Python Friday #19: Working with Packages in Python”