- The Python Standard Library Library
- Importing modules
- 3rd-party libraries
- Installing and using 3rd-party libraries
- Further reading
"Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it." ~ Larry Wall, creator of Perl
Programmers make a virtue of laziness. They routinely go out of their way to simplify complex tasks and automate mundane, repetitive work. They avoid reinventing wheels.
One way programmers accomplish the above is by writing reusable libraries, or packages, of code. Below we'll explore two avenues for making use of libraries.
Python itself ships with a large standard library that helps with myriad programming tasks. These include:
- Downloading remote files
- Working with local files and directories
- Reading and writing a range of data formats such as CSV, JSON and XML
- Working with dates and times
- Pattern matching in text
The list goes on and on...Pythonistas like to say that the language comes with "batteries included."
Making use of the standard library simply requires you to import a given library or module (the latter is the technical way to refer to a library in Python).
You can import and use modules inside a python script or the interactive interpreter.
Let's give it a try. Open a Python interactive interpreter to follow along (type python
or ipython
if the latter is installed).
Below we show how to import the json module to convert a Python dictionary to JSON, a common data format used in web programming.
>>> place = {'state': 'CA', 'city': 'Palo Alto'}
>>> import json
>>> json.dumps(place)
'{"state": "CA", "city": "Palo Alto"}'
Alternatively, you could directly import the dumps function using the from [module] import [name]
syntax.
>>> from json import dumps
>>> place = {'state': 'CA', 'city': 'Palo Alto'}
>>> dumps(place)
'{"state": "CA", "city": "Palo Alto"}'
For more details on importing modules, check out:
- W3C Python Modules
- Importing Modules section of Chapter 2 in Automate the Boring Stuff
As a Python programmer, you also have access to more than 200,000 libraries built by third-party developers through the Python Package Index. Many of these libraries supplement the standard library, while some improve upon libraries already built into the langauge. The quality and usefulness varies, but in a number of areas, "best of breed" libraries have emerged that are frequently used by journalists. They include libraries for:
- Downloading files from the web and working with APIs
- Scraping web sites
- Analyzing and visualizing data
- Using computer vision
- Working with geospatial data
- Machine learning
If you can think of a task, Python most likely has a library for it.
Unlike the standard library, you must first install a third-party library before you can import and use it in your code.
Standard package management tools such as pip and pipenv can be used to install third-party libraries. In this course, we generally use pipenv
to install libraries into "virtual environments" that allow us to isolate software dependencies for each project.
For example, to install the requests library into awesome-project
:
cd /path/to/awesome-project
pipenv install requests
Once installed in the virtual environment, you can import and use modules in the same way as described above. Just don't forget to activate the virtual environment before running a script or the interactive interpreter!!
cd /path/to/awesome-project
# Activate the virtual environment
pipenv shell
# Open an interactive Python interpeter
python
Now you should be inside the interactive Python interpreter, where you can import and use the requests module.
>>> import requests
>>> response = requests.get('http://example.com/')
See here for more details on using requests.
Importing and using modules
- Importing Modules section of Chapter 2 in Automate the Boring Stuff
- Hitchhiker's Guide to Python - Modules - a bit technical, but solid overview of modules and good habits when importing code
- The import system contains the gory details on importing modules
Python Standard Library
- A Brief Tour of the Python Standard Library - a gentler introduction to a wide range of useful tools in the standard library
- The Python Standard Library - full index of everything in the standard library