python manifest作用&__init__.py撰写技巧

佚名 / 2024-10-23 / 原文

python manifest作用&init.py撰写技巧

content

What is a Manifest File in Python?

A manifest file (in the context of Python) is typically a file named MANIFEST.in used when packaging Python projects with setuptools. It tells Python's build tools what extra files should be included in the package distribution beyond the Python code (like data files, documentation, or configuration files).


Purpose of MANIFEST.in

When building a Python package, setup.py only includes Python files by default. The MANIFEST.in file is used to specify additional files to include, such as:

  • Documentation (README.md)
  • License (LICENSE.txt)
  • Configuration files
  • Static resources (like HTML, CSS, JSON files)

Example of MANIFEST.in

include README.md  # Include README.md in the package
include LICENSE.txt  # Include LICENSE file
recursive-include docs *.md  # Include all Markdown files in the docs folder
recursive-include data *.json  # Include all JSON files in the data folder
exclude *.pyc  # Exclude compiled Python files
  • include: Add specific files.
  • recursive-include: Add files from a directory and its subdirectories.
  • exclude: Exclude specific files.

This ensures that when someone installs your package via pip, they also get the necessary non-Python files required to run or understand the package.


How to Write a Nice __init__.py

The __init__.py file is the entry point for a Python package. It can be empty, but a well-written __init__.py helps organize the package, provides useful metadata, and makes the package easy to use.

Purpose of __init__.py

  • Mark the folder as a Python package.
  • Control what gets imported when the package is imported.
  • Initialize package-wide variables or settings.
  • Simplify imports for users by defining what to expose directly.

Tips for Writing a Good __init__.py

1. Minimal Example: Mark the Package

# __init__.py (Minimal)
# This file is intentionally left empty
  • This is a valid empty package marker for Python.

2. Define Public API with __all__

Use the __all__ variable to control what symbols (functions, classes, etc.) are imported when someone uses:

from mypackage import *

Example:

# __init__.py
__all__ = ["function1", "function2"]

from .module1 import function1
from .module2 import function2

This way, only the functions listed in __all__ will be available.


3. Re-export Functions for Easier Imports

You can simplify imports for users by re-exporting specific functions or classes from submodules.

Without __init__.py:

from mypackage.module1 import function1

With __init__.py:

# __init__.py
from .module1 import function1

Now, users can:

from mypackage import function1

4. Add Package Metadata (Optional)

You can store package metadata (like the version) in __init__.py.

# __init__.py
__version__ = "1.0.0"
__author__ = "Your Name"

Users can now access this metadata:

import mypackage
print(mypackage.__version__)

5. Perform Package Initialization

You can execute initialization code when the package is imported. Be careful with heavy operations, as they will run every time the package is imported.

# __init__.py
print("Initializing the mypackage package")

Summary of a Well-Written __init__.py

Here’s a more complete example:

# __init__.py
__version__ = "1.0.0"
__author__ = "Your Name"
__all__ = ["function1", "function2"]

from .module1 import function1
from .module2 import function2

# Optional: Initialization code
print(f"Package mypackage (v{__version__}) initialized")

This approach:

  1. Re-exports useful functions and classes for easy access.
  2. Defines metadata to keep versioning organized.
  3. Includes optional initialization code.

Conclusion

  • MANIFEST.in ensures non-code files are included in your Python package distribution.
  • A good __init__.py makes the package easy to use, well-organized, and provides essential information like versioning.

With these strategies, your packages will be more user-friendly and well-maintained.