Understanding Module vs. Package in Python
In Python, the distinction between a module and a package is primarily observed at the file system level. Let's delve into their key differences:
While this distinction is evident in file system organization, it's important to note that when you import a module or package, the corresponding object created by Python is always of the type "module." However, in the case of importing a package, only variables, functions, and classes defined in the package's "__init__.py" file are directly accessible, not sub-packages or sub-modules.
Example
Consider the "xml" package in the Python standard library:
When importing the package interactively:
import xml
You notice that the "xml" object is of type "module." Accessing sub-components requires further imports:
import xml.etree import xml.etree.ElementTree
Note: Python also includes built-in modules like "sys" that are implemented in C, but this distinction isn't typically included in discussions about modules and packages.
The above is the detailed content of What\'s the Difference Between a Python Module and a Package?. For more information, please follow other related articles on the PHP Chinese website!