Python Module System and Project Organization
Learn Python's module system, package management, and project organization best practices from a JavaScript developer's perspective.
1. Introduction
Why Do We Need a Module System?
In front-end development, we are used to organizing code with ES6 modules or CommonJS. Python also has its own module system. Although the syntax is different, the core concepts are similar.
Core Value of Modularity
- Code reuse: Avoid writing the same functionality repeatedly
- Namespace isolation: Avoid variable name conflicts
- Project organization: Group related functionality together
- Dependency management: Clearly define project dependencies
💡 Learning Strategy: Think of Python's module system as a "dialect version" of the JavaScript module system.
2. Python Module System Basics
2.1 What is a Module?
In Python, any .py
file can be a module. This is similar to a single .js
file in JavaScript.
2.2 Ways to Import Modules
Python provides several ways to import modules, each with its own use case.
Import Method Comparison Table
Function | JavaScript | Python | Description |
---|---|---|---|
Import entire module | import * as Module | import module | Python is more concise. |
Named import | import { func } | from module import func | Different syntax, but the same concept. |
Rename | import { func as newName } | from module import func as newName | Both support it. |
Default import | import defaultFunc | No corresponding concept | Python has no default export. |
Namespace import | import * as Namespace | import module as namespace | Same concept. |
2.3 Special Variables in Modules
Python modules have some special variables, similar to some global variables in JavaScript.
3. Package System
3.1 What is a Package?
A package is a directory containing multiple modules, similar to an npm package or directory structure in JavaScript.
3.2 Importing and Using Packages
4. Project Organization Best Practices
4.1 Project Directory Structure
A good project structure is crucial for code maintenance and team collaboration.
4.2 Dependency Management Comparison
5. Practical Project Example
5.1 Creating a Simple Calculator Package
Let's create a complete calculator package to demonstrate the practical application of the module system.
5.2 Using the Calculator Package
6. Advanced Module Features
6.1 Relative Imports
Python supports relative imports, similar to relative path imports in JavaScript.
6.2 Dynamic Imports
Python supports dynamic module imports, similar to JavaScript's dynamic import()
.
7. Exercises
Exercise 1: Create a Utility Package
Create a utility package that includes math and string utilities.
Exercise 2: Module Import Exercise
8. Summary
Key Concept Review
- Module: Any
.py
file can be used as a module. - Package: A directory containing an
__init__.py
file, which can contain multiple modules. - Import Methods:
import
,from ... import
,as
for renaming. - Special Variables:
__name__
,__file__
,__all__
. - Project Organization: Reasonable directory structure and dependency management.
JavaScript vs. Python Comparison Summary
Concept | JavaScript | Python | Description |
---|---|---|---|
Module File | .js file | .py file | Both use a single file as a module. |
Package | Directory + package.json | Directory + __init__.py | Python requires __init__.py . |
Import Syntax | import { func } | from module import func | Different syntax, but the same concept. |
Default Export | export default | No corresponding concept | Python has no default export. |
Relative Import | ../utils/math | ..utils.math_utils | Both support relative paths. |
Dynamic Import | import() | importlib.import_module() | Both support runtime imports. |
Next Steps
In the next module, we will learn about:
- Object-oriented programming (classes, inheritance, polymorphism)
- Functional programming features
- Decorators and metaprogramming
- Special methods (magic methods)
These concepts will help you gain a deeper understanding of Python's programming paradigms and enable you to write more complex and elegant code.
JavaScript vs Python Syntax Comparison
An in-depth comparison of the syntax differences between the two languages from a JavaScript developer's perspective, to quickly master Python syntax.
Python Object-Oriented & Functional Programming
Learn Python's object-oriented programming, functional programming features, and decorators from a JavaScript developer's perspective.