programming:python:packaging
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programming:python:packaging [2018-07-22 23:21] – [Simple Importing] add note about __all__ zlg | programming:python:packaging [2023-11-06 20:36] (current) – add notice about Python community's kerfuffle about packaging zlg | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Packaging ====== | ====== Packaging ====== | ||
- | In Python, individual source files are referred to as **modules**. They represent a single collection of code, and can be '' | + | FIXME :!: The Python community has some PEPs and a lot of debate over what the ' |
+ | |||
+ | In Python, individual source files are referred to as **modules**. They represent a single collection of code, and can be '' | ||
Here's an example of a //package// containing // | Here's an example of a //package// containing // | ||
Line 14: | Line 16: | ||
</ | </ | ||
- | There are multiple ways to interact with a package, which are covered below. In every case, it's assumed that the Python interpreter is in the directory //above// '' | + | There are multiple ways to interact with a package, which are covered below. In every case, it's assumed that the Python interpreter is in the directory //above// '' |
===== Simple Importing ===== | ===== Simple Importing ===== | ||
Line 32: | Line 34: | ||
The '' | The '' | ||
- | If every module is needed in your package, you can leave '' | ||
- | |||
- | The same effect can be achieved using the '' | ||
- | |||
- | <code python __init__.py> | ||
- | # Overload the __all__ variable to hook into existing Python logic | ||
- | __all__ = [" | ||
- | </ | ||
===== Single Module Importing ===== | ===== Single Module Importing ===== | ||
Line 60: | Line 54: | ||
</ | </ | ||
- | In the above example, '' | + | In the above example, '' |
+ | |||
+ | If you find yourself | ||
<code python> | <code python> | ||
Line 67: | Line 63: | ||
</ | </ | ||
+ | This is often caused by importing two modules with the same member name. If you import two things with the same member name, the latter name will take precedence: | ||
+ | |||
+ | <code python> | ||
+ | # Same name, different structures | ||
+ | from os import path | ||
+ | print(type(path)) | ||
+ | # <class ' | ||
+ | from sys import path | ||
+ | print(type(path)) | ||
+ | # <class ' | ||
+ | </ | ||
+ | |||
+ | For this reason, it's generally better to leave imports alone and go one level deeper only as needed. So if you wanted to use '' | ||
====== References ====== | ====== References ====== | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | |||
+ | ~~DISCUSSION~~ |
programming/python/packaging.1532301689.txt.gz · Last modified: 2018-07-22 23:21 by zlg