Modules & Libraries Jump to this section
Modules and libraries let you reuse code other people wrote so you do not have to build everything from scratch.
Modules vs Libraries Jump to this section
- A "module" is a single file of Python code (for example
math.py). - A "library" is a collection of modules that work together to provide functionality (for example the
requestslibrary contains modules for HTTP).
Python also includes a standard library, which is a set of built-in modules that come with Python. Even though these modules are included with Python, you still need to import them before using them in your code.
Examples of standard library modules: math, random, datetime, json.
Third-party libraries are packages you install yourself (usually with pip) and then import in your code.
Importing a Module Jump to this section
Use import to bring code into your program. The standard library includes many useful modules.
import math
print(math.sqrt(16)) # 4.0
from math import sqrt
print(sqrt(25)) # 5.0
When you write import math, Python finds the math.py module for you.
You never include the .py extension when importing.
Using import math makes it clear where a function comes from (math.sqrt),
which is often easier to read in larger programs (to avoid not knowing where sqrt() came from).
Another example:
import random
print(random.choice(["red", "green", "blue"]))
mathandrandomare built into python and no installation is needed; you can try them on the Python playground.
Aliases Jump to this section
You can give a module a shorter or more convenient name when importing it.
This is called creating an alias and is done with the as keyword.
import math as m
print(m.sqrt(9))
In this example:
mathis the module’s real namemis an alias that refers to the same modulem.sqrtworks exactly the same asmath.sqrt
Installing Third-Party Packages with pip
Jump to this section
When you need extra functionality, install packages from the Python Package Index (PyPI) using pip.
Basic commands (using the requests library as an example):
pip install requests
pip show requests
pip uninstall requests
If your system has multiple Python versions you may need pip3 instead of pip.
If pip is not found, try:
python -m pip install requests
Installing a package and importing it are two different steps:
- Installing (
pip install) puts the code on your computer - Importing (
import) makes it available in your program
Installing packages does not change Python itself; it just adds extra tools.
Virtual Environments Jump to this section
A virtual environment is a folder that holds the packages used by a specific project. Each project can have its own virtual environment with its own versions of libraries.
You don’t need to fully understand virtual environments yet.
Just know they help keep projects isolated and avoid conflicts.
python -m venv venv # create a virtual environment named 'venv'
# Windows
venv\Scripts\activate # 'Scripts' holds the virtual environment’s executables (python, pip, activate)
# macOS / Linux
source venv/bin/activate # macOS / Linux: 'bin' (binary) holds the virtual environment’s executables
Once activated, any packages you install with pip will be installed only inside this environment:
pip install requests
When you are finished working on the project, deactivate the environment:
deactivate
Using Installed Packages Jump to this section
After installing requests, import and use it:
import requests
resp = requests.get('https://api.github.com')
print(resp.status_code)
print(resp.json().get('current_user_url'))
This example makes a real network request, so it may fail without internet access.
Finding Documentation Jump to this section
When using libraries, you can check the package documentation for usage examples.
- Read the project's README on its PyPI or GitHub page.
- Use
help(module)inside Python for quick docs:
import math
help(math)
Challenge Jump to this section
Write a short script that uses the math module to print the area of a circle when the user types the radius. Use input() and convert to a number.
Try it yourself first.
Hint
input() returns a string, so convert it to float.
Area of a circle is pi * r * r. Use math.pi for a value of pi.
Show example solution
import math
radius = float(input("Radius: "))
area = math.pi * radius ** 2
print(f"Area: {area:.2f}") # round area to 2 decimal places
Key Takeaways Jump to this section
- Use
importto reuse code from the standard library and third-party packages. - Install packages with
pipand prefer using a virtual environment per project. - Read package documentation for usage details and examples.
- Use
help()in Python or online docs when unsure.
