Libraries and packages
Libraries are prepackaged pieces of reuseable code that you’re free to use in your Python code. Someone else wrote them, and you get to use them. They’re also known as packages, modules, and a million other words.
For example, maybe you’d like to round up or round down? It isn’t built-in to normal Python, but the module named math
can give you a hand.
import math
math.ceil(3.14) # 4
math.floor(3.14) # 3
Examples of packages
requests
allows you to download web pages.pg8000
lets Python connect to a PostgreSQL database.BeautifulSoup
is used for reading the content on downloaded web pages.pandas
helps with manipulating and understanding csv files.
There are ten million more, many of which you can find on the Awesome Python repository.
Installing packages with pip
The math
module comes with Python, but for most every other package you’ll need to install it separately.
pip
is a package manager for Python, in that you can use it to… install packages. In fact, pip
stands for Pip Installs Packages
! To use pip
to install a package, run a command like the following from the command line.
pip install requests
IF YOU’RE USING VIRTUAL ENVIRONMENTS and run pip
, you’ll only be installing the package for the current virtual environment. If you have three Python 3 data analysis virtual environments you’ll need to workon
each of them and pip install pandas
three separate times.
IF YOU AREN’T USING VIRTUAL ENVIRONMENTS, you’ll want to use pip3
, which will make sure you’re installing the Python 3 version of the packages. If you’d like to use a package inside of Python 2, you’ll want to use pip
.
Using packages
In order to use a package, you’ll need to tell Python that you’d like to include it in your code. You do this by import
ing the package up at the top of your code.
import requests
import pandas as pd
from bs4 import BeautifulSoup
Why do each of those ways of importing look different? Well… that’s just kind of how life works. Usually you should just copy what everyone else is doing in documentation or on StackOverflow, but if you wanted to know a little more…
Simple imports
Once you import a library, you usually just refer to it by its name when you’d like to use it.
# Import requests library
import requests
# Download Drake's top tracks on Spotify
requests.get('https://api.spotify.com/v1/artists/4W9G3Vnt9eXWTo4VeOQkSa/top-tracks?country=US')
Renaming your import
People don’t like to type out the word pandas
, instead they like to type out pd
. So when they import it, they rename it.
# Import pandas library,
# but we want to call it pd
import pandas as pd
# Import a csv of superbowl data
# save it into a variable called df
df = pd.read_csv('superbowls.csv')
Importing sub-modules
Sometimes the code you want is one part of a package, so you specifically pick that one module out.
# Import requests normally, but
# import BeautifulSoup from inside bs4
import requests
from bs4 import BeautifulSoup
# Use requests to download a webpage
result = requests.get('https://www.nytimes.com')
# Use BeautifulSoup to analyze the content of the page
doc = BeautifulSoup(result.content, 'html.parser')
Finding libraries, modules and packages
Google is your friend! The best and worst thing about packages is that anyone can write them, which means you’ll have great ones and terrible ones and popular ones and unpopular ones.
How do you know which one to pick? Again, google.
If you’d like to be jealous look at Ruby Toolbox, which shows the most popular packages for the programming language Ruby. It also shows not only how often they’re being used, but how often they’re being updated and change over time!
With Python you just kind of have to cross your fingers and hope you’re picking the right one. Alas!