Skip to content

Latest commit

 

History

History
125 lines (80 loc) · 6.22 KB

libraries.md

File metadata and controls

125 lines (80 loc) · 6.22 KB

Libraries

Overview

"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 Standard Library

Python itself ships with a large standard library that helps with myriad programming tasks. These include:

The list goes on and on...Pythonistas like to say that the language comes with "batteries included."

Importing modules

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:

Third-party libraries

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:

If you can think of a task, Python most likely has a library for it.

Installing and using libraries

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.

Further reading

Importing and using modules

Python Standard Library