Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Modules vs Packages | Python Project Structure & Packaging Basics
Python Packaging and Distribution

bookModules vs Packages

Note
Definition

A Python module is a single file that contains Python code such as variables, functions, and classes, which can be imported and reused in other programs. It helps organize code, avoid repetition, and keep projects easier to maintain.

You will often hear the terms module and package when organizing Python code. Understanding the difference between these two is essential for structuring any Python project efficiently. A module is simply a single Python file ending with .py that contains functions, classes, or variables. For example, a file named my_module.py with a greet function is a module.

123
# my_module.py def greet(name): return f"Hello, {name}!"
copy

A package, on the other hand, is a directory (folder) that contains an __init__.py file. This special file tells Python that the directory should be treated as a package, allowing you to group related modules and sub-packages together. For instance, a directory called my_package containing an __init__.py file and other Python files such as utils.py forms a package. The __init__.py file can be empty or can include initialization code for the package.

__init__.py

__init__.py

utils.py

utils.py

copy

Use a module when you have a small, self-contained set of functionality; use a package when you need to organize multiple related modules or want to build a larger, more maintainable project. Packages help you avoid name conflicts and make your codebase easier to navigate as it grows.

question mark

What is the main difference between a Python module and a package?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you give more examples of modules and packages?

How do I import a module or package in my Python code?

What is the purpose of the __init__.py file in a package?

bookModules vs Packages

Glissez pour afficher le menu

Note
Definition

A Python module is a single file that contains Python code such as variables, functions, and classes, which can be imported and reused in other programs. It helps organize code, avoid repetition, and keep projects easier to maintain.

You will often hear the terms module and package when organizing Python code. Understanding the difference between these two is essential for structuring any Python project efficiently. A module is simply a single Python file ending with .py that contains functions, classes, or variables. For example, a file named my_module.py with a greet function is a module.

123
# my_module.py def greet(name): return f"Hello, {name}!"
copy

A package, on the other hand, is a directory (folder) that contains an __init__.py file. This special file tells Python that the directory should be treated as a package, allowing you to group related modules and sub-packages together. For instance, a directory called my_package containing an __init__.py file and other Python files such as utils.py forms a package. The __init__.py file can be empty or can include initialization code for the package.

__init__.py

__init__.py

utils.py

utils.py

copy

Use a module when you have a small, self-contained set of functionality; use a package when you need to organize multiple related modules or want to build a larger, more maintainable project. Packages help you avoid name conflicts and make your codebase easier to navigate as it grows.

question mark

What is the main difference between a Python module and a package?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
some-alt