python-backend
/

Python Modules & Packages – Organizing Large Projects

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Python Modules & Packages – Organizing Large Projects

What are Modules and Packages?

As your program grows, keeping all code in one file becomes unmanageable. A Module is simply a file containing Python definitions and statements. A Package is a collection of modules organized in a folder hierarchy. This modularity allows for code reuse and better organization, similar to how you use 'exports' and 'imports' in Flutter or JavaScript.

Importing Modules

You can use any Python source file as a module by executing an import statement in another source file. Python searches for these modules in the current directory, then in the installed libraries path.

PythonRead-only
1
# 1. Import the whole module
import math
print(math.sqrt(16))

# 2. Import specific functions (cleaner names)
from datetime import datetime
print(datetime.now())

# 3. Import with an alias
import pandas as pd
import numpy as np

Creating Your Own Module

Create a file named calculator.py. This is now a module that can be imported into other files in the same directory.

PythonRead-only
1
# calculator.py
def add(a, b):
    return a + b

# main.py
import calculator
result = calculator.add(5, 3)

The Role of init.py

To turn a directory into a Package, it traditionally required a file named __init__.py. While modern Python (3.3+) supports 'Namespace Packages' without it, __init__.py is still used to initialize package-level data or to simplify complex imports.

TEXTRead-only
1
my_app/
├── main.py
└── utils/
    ├── __init__.py
    ├── helpers.py
    └── validators.py

Standard Library vs. Third-Party

CategorySourceExamples
Built-inPython Coreprint(), len(), range()
Standard LibraryIncluded with Pythonos, sys, json, math, re
Third-PartyPyPI (via pip)requests, flask, tensorflow, pandas

Try it yourself

import math
import random

# Using the math module
radius = 7
area = math.pi * math.pow(radius, 2)
print(f"Area of circle: {area:.2f}")

# Using the random module
tech_list = ["Flutter", "Python", "React", "Node"]
choice = random.choice(tech_list)
print(f"Today we are studying: {choice}")

Test Your Knowledge

Q1
of 3

Which keyword is used to bring a module into your current file?

A
include
B
require
C
import
D
using
Q2
of 3

How do you import only the 'sqrt' function from the 'math' module?

A
import sqrt from math
B
from math import sqrt
C
import math.sqrt
D
get sqrt from math
Q3
of 3

What is the standard name of the file used to initialize a Python package?

A
main.py
B
package.py
C
__init__.py
D
__start__.py

Frequently Asked Questions

What is 'if __name__ == "__main__":'?

This idiom allows you to write code that only runs when the file is executed directly, not when it is imported as a module in another file.

How do I see all functions in a module?

You can use the built-in dir() function. For example, 'import math; print(dir(math))' will list everything inside the math module.

What is a 'Circular Import'?

This happens when Module A imports Module B, and Module B also tries to import Module A. This causes an error and should be avoided by restructuring the code.

Previous

python oop

Next

python error handling

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.