Modules & Packages
As programs grow, keeping every line of code in a single file becomes unmanageable. Python solves this with modules and packages — a way to split code into separate files and folders, and then reuse it wherever it is needed.
What is a Module?
A module is simply a .py file. Any Python file you write can be imported and reused by another file. Suppose you have a file named mymodule.py with the following content:
# mymodule.py
def greet(name):
return f"Hello, {name}!"
PI = 3.14159
You can bring that entire module into another file with import, then access its contents using dot notation:
import mymodule
print(mymodule.greet("Sonu")) # Hello, Sonu!
print(mymodule.PI) # 3.14159
If you only need one specific name from a module, import it directly with from ... import ... so you can call it without the module prefix:
from mymodule import greet
print(greet("Sonu")) # Hello, Sonu!
Importing with an Alias
The as keyword lets you import a module under a shorter or more convenient name, which is especially useful for modules with long names that get used constantly throughout a file.
# a very common convention in the data science world
import numpy as np
# now you refer to it as "np" instead of "numpy"
# arr = np.array([1, 2, 3])
Note: numpy is a hugely popular third-party library for numerical computing, and import numpy as np is the near-universal convention you will see in real code. It is not installed by default with Python itself, so the snippet above is shown purely to illustrate the aliasing pattern.
The Python Standard Library
Python ships with a huge collection of ready-to-use modules called the Standard Library, so you rarely have to write common functionality from scratch. A few modules you will run into constantly:
| Module | What it's for |
|---|---|
math | Mathematical functions like square roots, trigonometry, and constants such as math.pi. |
random | Generating random numbers, shuffling lists, and picking random items. |
datetime | Working with dates and times. |
os | Interacting with the operating system: file paths, environment variables, directories. |
import math
import random
print(math.sqrt(16)) # 4.0
print(random.randint(1, 10)) # a random integer between 1 and 10
Packages: Folders of Modules
A package is a folder that groups related modules together. What makes a folder a package (rather than just a plain folder) is the presence of a special __init__.py file inside it — this file can be empty, but its presence tells Python "treat this folder as an importable package."
# example package layout
mypackage/
__init__.py
shapes.py
colors.py
Once laid out this way, modules inside the package are imported using dotted paths:
from mypackage import shapes
from mypackage.colors import hex_to_rgb
Installing Third-Party Packages with pip
Not everything you need ships with Python itself. For third-party libraries, you use pip, Python's package installer, to download and install them from the Python Package Index (PyPI). See the Installation & Setup page for a closer look at setting up pip and virtual environments.
# install a third-party package
pip install requests
The if __name__ == "__main__": Idiom
Every Python module has a built-in variable called __name__. When a file is run directly, Python sets __name__ to "__main__". When that same file is instead imported by another file, __name__ is set to the module's own name instead. This lets you write code that only runs when the file is executed directly, not when it is imported for its functions and classes.
# mymodule.py
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
# this block only runs when you execute "python mymodule.py" directly
print(greet("World"))
If another file does import mymodule, the print statement above will not run — only the greet function definition is loaded. This keeps test code, demos, or script-only behavior from accidentally firing whenever a module is reused elsewhere.