理解module, script, library, package in Python

  • Overview

    Python modules and Python packages are two mechanisms that facilitate modular programming.

    A script is a Python file that’s intended to be run directly.

    A module is a Python file that’s intended to be imported into scripts or other modules.

    A package is a collection of related modules that work together to provide certain functionality.

    A library is an umbrella term that loosely means “a bundle of code”. These can have tens of even hundreds of individual modules that can provide a wide range of functionality.

    • Matplotlib is a plotting library.
    • The Python Standard Library contains hundreds of modules for performing common tasks

    Only package and module have a well-defined meaning specific to Python.

  • Module

    There are actually three different ways to define a module in python:

    1. A module can be written in Python itself
    2. A module can be written in C and loaded dynamically at run-time, like the re(regular expression) module
    3. A built-in module is intrinsically contained in the interpreter, like the itertools module.

    A module’s contents are accessed the same way in all three cases: with the import statement.

    When the interpreter executes the import statement, it searches for file.py in a list of directories which is called search path that can be accessible in the Python variable sys.path assembled from the following sources:

    • The directory from which the input script was run or the current directory if the interpreter is being run interactively
    • The list directories contained in the PYTHONPATH environment variable
    • An installation-dependent list of directories configured at the time Python is installed
    import re
    re.__file__	# 查看module位置
    

    import module_name does not make the module contents directly accessible to the caller. Each module has its own private symbol table, which serves as the global symbol table for all objects defined in the module, Thus a module creates a separate namespace.

    The built-in function dir() returns a list of defined names in a namespace.

  • Script

    Any .py file that contains a module is essentially also a Python script, and there isn’t any reason it can’t be executed like one.

    $ python file.py
    

    First its execute a module file.py

    When a .py file is imported as a module, Python sets the special dunder variable __name__ to the name of the module. However, if a file is run as a standalone script, __name__ is set to the string "__main__".

    Modules are often designed with the capability to run as a standalone script for purpose of testing the functionality that is contained within the module. This is referred to as unit testing.

  • Package

    A package is a collection of modules in directories.

    Packages allow for a hierarchical structuring of the module namespace using dot notation.

    In the same way that modules help avoid collisions between global variable names, packages help avoid collisions between module names.

    Package use the OS’s inherent hierarchical file structure.

    If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. This can be used for execution of package initialization code, such as initialization of package-level data.

    Python follows this convention: if the __init__.py file in the package directory contains a list named __all__, it is taken to be a list of modules that should be imported when the statement from packageName import * is encountered

  • References

  1. Real Python: Python Modules and Packages – An Introduction
  2. Real Python: Scripts, Modules, Packages, and Libraries
  3. StackOverflow: Whats the difference between a module and a library in Python?
  4. Python Docs: Modules

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/115023634