Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wimglenn/resources-example
An example project demonstrating how to access data files in Python package
https://github.com/wimglenn/resources-example
Last synced: 4 days ago
JSON representation
An example project demonstrating how to access data files in Python package
- Host: GitHub
- URL: https://github.com/wimglenn/resources-example
- Owner: wimglenn
- Created: 2019-10-25T03:04:08.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T02:17:22.000Z (5 months ago)
- Last Synced: 2024-10-13T09:14:14.156Z (3 months ago)
- Language: Python
- Size: 12.7 KB
- Stars: 81
- Watchers: 4
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
This project shows how to package data files within a Python distribution, and has some example code for reading the data files. To build this distribution, create a venv with build_ installed and then execute
.. code-block:: bash
python -m build
The distributions (an sdist .tar.gz and a bdist .whl) will be written to ./dist/ subdirectory. To test it out, install the distribution and run the console script ``resources-example``.
Here's a compatibility summary of the five approaches demonstrated:
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+
| Module | Description | In stdlib? | Works on Py2? | Works on Py3? | Works in zipfile? | Run as script? [*]_ |
+=============+=======================+============+===============+===============+===================+=====================+
| example1.py | os.path.join | yes | yes | yes | no | yes |
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+
| example2.py | pkgutil | yes | yes | yes | yes | no |
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+
| example3.py | pkg_resources | no | yes | deprecated | yes | yes |
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+
| example4.py | importlib.resources. | deprecated | no | yes (3.7+) | yes | yes |
| | read_binary/read_text | | | | | |
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+
| example5.py | importlib.resources. | yes (3.9+) | yes [*]_ | yes | yes | yes |
| | files | | | | | |
+-------------+-----------------------+------------+---------------+---------------+-------------------+---------------------+If you are interested in creating an executable zip from source, you can use stdlib `zipapp `_ utility (Python 3.5+):
.. code-block:: bash
python3 -m zipapp --compress /path/to/resources-example --main="myapp:main" --output=myapp.zip
If this command is slow or the .zip is surprisingly large, make sure don't have any stray subdirs in the source path beforehand (e.g. ``.venv``, ``.git``, ``.idea``).
Now you can run the zip directly with the interpreter (any Python version):
.. code-block:: bash
python myapp.zip
.. _build: https://pypi.org/project/build/
.. [*] "Run as script" means executing the submodule directly, e.g. ``python myapp/example2.py``. Note that Guido considers this `an anti-pattern `_.
.. [*] The same APIs are available in 2.7 by using an `importlib_resources `_ backport.