What's the difference between a Python module and a Python package?

前端 未结 5 1186
难免孤独
难免孤独 2020-11-22 11:46

What\'s the difference between a Python module and a Python package?

See also: What's the difference between "package" and "module" (for othe

5条回答
  •  遥遥无期
    2020-11-22 12:15

    From the Python glossary:

    It’s important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a __path__ attribute is considered a package.

    Python files with a dash in the name, like my-file.py, cannot be imported with a simple import statement. Code-wise, import my-file is the same as import my - file which will raise an exception. Such files are better characterized as scripts whereas importable files are modules.

提交回复
热议问题