beginner22 min

Modules, Packages, and Virtual Environments

Understand how import works, what pip and virtual environments are for, and how Python code is organized beyond a single file.

What you'll learn

  • Explain what a module is and how import makes its code available
  • Describe what a virtual environment is and why projects use one each
  • Explain the role of pip and a requirements file for installing packages
  • Use a built-in module (math or random) inside a program

Prerequisites

Explanation

Every program you've written so far has lived in a single file. Real projects quickly outgrow that — logic gets split across many files so it stays organized and reusable. Python's mechanism for this is the module.

Modules. A module is just a .py file containing Python code — functions, variables, classes. Any file can use another module's code with import module_name, which runs that file once and makes everything it defines available as module_name.something. Python ships with a large standard library of built-in modules you can import without installing anything, including math (mathematical functions and constants), random (pseudo-random number generation), datetime, and many more.

Writing import math then calling math.sqrt(81) keeps things organized: you always know sqrt came from math rather than colliding with some other function of the same name. You can also write from math import sqrt to pull a name in directly, at the cost of that clarity — this is why from module import * (importing everything) is generally discouraged, since it makes it unclear where a given name came from and risks silently overwriting names you already have.

Packages. A package is a folder of related modules, distributed and installed as one unit. When you install someone else's package, you're bringing in code you didn't write, so it can immediately provide capabilities — talking to a web API, working with images, running a machine learning model — that would take far longer to build from scratch.

pip. Python's standard package installer, pip, downloads packages from the Python Package Index (PyPI) and installs them into your current Python environment. Running pip install requests, for example, fetches the popular requests package so any script in that environment can then import requests. Projects commonly list their dependencies in a requirements.txt file so anyone (including a teammate, or a deployment server) can recreate the exact same set of installed packages with one command.

Virtual environments. Different projects on the same machine often need different, sometimes conflicting versions of the same package. A virtual environment (created with python -m venv) is an isolated, self-contained copy of Python plus its own separate folder of installed packages, so installing something for one project never affects another. You "activate" a virtual environment before working on a project, install what that project needs inside it, and everything stays contained. This is standard practice in essentially every real Python project, from small scripts to production backends.

Because this course runs in a browser-based sandbox with no ability to install external packages, the exercises below use only modules already built into Python — but the import, pip, and venv concepts here are exactly what you'll use the moment you set up Python on your own machine.

How a project's dependencies stay organized

Project folder → virtual environment (its own isolated copy of Python + installed packages) → requirements.txt lists what pip should install → import brings a module's code into your script, whether it's a built-in standard-library module or an installed third-party package.

Example

Using the built-in math module to compute a circle's area and circumference.

import math

radius = 4
area = math.pi * radius ** 2
circumference = 2 * math.pi * radius

print(f"Area: {area:.2f}")
print(f"Circumference: {circumference:.2f}")
print("Square root of 81:", math.sqrt(81))

Try it yourself

Change radius to a different value, then press Run.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Use random.randint(1, 100) to roll a number and store it in roll. The seed is fixed so the result is reproducible.

Checks: roll matches the seeded random.randint(1, 100) call · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Write average(numbers) using sum() and len(), and std_dev(numbers) which uses average() and math.sqrt() to compute the population standard deviation.

Checks: average of the sample dataset is 5.0 · std_dev of the sample dataset is 2.0 · average([10]) is 10.0 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Confusing 'import' (loading code that's already available) with 'installing a package' (pip fetching new code from the internet) — import alone can't get you code you never installed.
  • Using from module import * and then being unable to tell which module a name actually came from once bugs appear.
  • Skipping virtual environments and installing everything globally, which eventually causes version conflicts between unrelated projects.
  • Forgetting to activate a project's virtual environment before installing packages, so they end up installed somewhere else entirely.

Knowledge check

Knowledge check

1. What does running pip install requests actually do?
2. What problem do virtual environments primarily solve?
3. What is the difference between import math and from math import sqrt?
4. What is a requirements.txt file conventionally used for?

Takeaway

import loads code that's already available, pip installs new packages from PyPI into your environment, and a virtual environment keeps each project's dependencies isolated from every other project's.

Summary

Modules are .py files whose code becomes available via import, and packages bundle related modules for distribution. pip installs packages from PyPI, typically tracked in a requirements.txt file, while virtual environments give each project its own isolated set of installed packages so versions never collide across projects.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.

Next: Files and Exceptions