The versatility of Python is a primary reason it has become one of the most popular programming languages. It is used across a wide range of disciplines, from doing fundamental physics research to machine learning (ML) and artificial intelligence (AI) to application development.
This widespread usage is enabled, in part, by an extensive standard library that offers a range of facilities designed to enhance the functionality and portability of Python. This is complemented with a growing number of packages and projects available through the Python Package Index (PyPI), which not only provide extended capabilities of Python to more domain-specific use cases, but also increase the general usability of Python. A list of the most popular packages downloaded from the Python Package Index is available here.
In this article, I’ll highlight my Top 10 packages (all of which can all be found on PyPI), and provide some basic examples of how they’re typically used.
The Most Useful Python Packages
Despite the many different use cases for programming in Python, there are several packages that are especially useful above all. Regardless of whether you’re using Python for ML or web apps, the following 10 packages are worth knowing and can only improve your experience with using Python. I’ll start with the absolutely necessary and end with the merely essential:
1. pip: As I discussed in a previous article, pip is the standard way of installing and managing packages in Python. Pip comes standard with every Python distribution, allowing you to accomplish installs, uninstalls, updates, etc from the command line. For example, to install a specific package with pip from PyPI, run:
pip install “SomePackage”
Or for a specific package version:
pip install “SomePackage == 1.0”
pip allows for installation from multiple sources, and is not limited to installing packages maintained on the PyPI. For more information, see the documentation here.
2. Six: Six is a Python 2 and 3 compatibility library, which is especially relevant given the amount of application migration from Python 2 to 3 organizations are currently undertaking due to Python 2’s end of life. Six reconciles the differences between Python 2 and 3, and makes adjustments based on which version is running locally. This allows Python programmers to write code that is compatible with both versions of Python, without too much difficulty.
For example, in Python 3, iterating dictionary keys is done by:
for item in dictionary.items():
In Python 2, iteration is done by:
for item in dictionary.iteritems():
With Six, the syntax is:
for item in six.iteritems(dictionary):
This code will successfully run on both Python 2 and 3. Additional examples can be found in the documentation.
3. python-dateutil: The dateutil module provides a number of date and time manipulation capabilities, such as computing relative differences between two arbitrary dates, parsing datetime objects, and handling time zone information. It builds on the datetime module that is built into Python, and is simple and easy to use.
For example, to get the current local time:
from datetime import *
from dateutil.relativedelta import *
now = datetime.now()
To add an arbitrary number of months, days, and hours:
now + relativedelta(months=1, weeks=1, hour=10)
Or to query when the next Wednesday will occur:
now + relativedelta(weekday=WE(+1))
These are just a few examples, but the general functionality follows this trend. The package is simple, but can dramatically improve your Python experience when handling time-series data. For more information, the documentation can be found here.
4. Requests: The requests package is an HTTP library for Python. It is built on top of urllib3 (another HTTP client for Python), but has a much simpler and more elegant syntax. It also integrates a few other Python libraries in order to maximize functionality while still managing to minimize complexity. Using urllib3 alone (or the built-in urllib and urllib2) allows for more customization and deeper control, but also requires more work on the side of the user. For this reason, requests is the preferred HTTP client for nearly all use cases in Python. The full list of features can be found here.
For example, here’s how to make a request to Spotify (no authentication required):
r = requests.get(‘https://api.spotify.com/’)
A status code in the 200’s indicates a success. From here we can extract the headers, the encoding, and a myriad of other information:
5. Docutils: The Documentation Utilities project exists to create a set of tools to easily process plaintext documents into more useful file formats such as HTML, XMS, or LaTeX. The project developed several front-end tools for the most common processes. This involves reading the input file (Reader tool), parsing appropriately (Parser tool), and writing the new file (Writer tool). The command line syntax for each of these tools follows a standard structure:
Dante is a former physicist who left the laboratory to join the ranks of scientists and engineers turning to techniques within data science to solve today’s problems. He is currently pursuing a Master's degree in Data Science for Complex Economic Systems. He lives in Turin, Italy.