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 requests library 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

import math keeps the module name (helpful to see where functions come from). from math import sqrt brings sqrt directly into your namespace.

Standard Library Examples Jump to this section

  • math: common math functions
  • random: random numbers
  • datetime: dates and times
  • json: read and write JSON data
import random
print(random.choice(["red", "green", "blue"]))

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:

pip install requests
pip show requests
pip uninstall requests

If your system has multiple Python versions you may need pip3 instead of pip.

Virtual Environments (recommended) Jump to this section

Use virtual environments to keep project dependencies separate.

python -m venv venv          # create a virtual environment named 'venv'
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate
pip install requests

Deactivate when done with 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'))

Be mindful of network errors and check the package documentation for usage examples.

Finding Documentation Jump to this section

  • Read the project's README on its PyPI or GitHub page.
  • Use help(module) inside Python for quick docs: import math; help(math).

Small 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

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}")

Key Takeaways Jump to this section

  • Use import to reuse code from the standard library and third-party packages.
  • Install packages with pip and prefer using a virtual environment per project.
  • Read package documentation for usage details and examples.
  • Use help() in Python or online docs when unsure.

Would you like a small section on common packages (requests, numpy, pandas) with short examples? I can add that next.