|
3 | 3 | Retrieving package version at runtime |
4 | 4 | ------------------------------------- |
5 | 5 |
|
6 | | -Using ``version_file`` or ``version_callback`` options |
7 | | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 | +Using ``version_file`` option (recommended) |
| 7 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
8 | 8 |
|
9 | | -The preferred way to set version number inside a package is |
10 | | -to simply store it in some file or variable/function, and |
11 | | -then use it in ``setup.py`` / ``pyproject.toml`` as version source. |
| 9 | +In case of using :ref:`version_file <version-file>` option you can directly read the ``VERSION`` file content, |
| 10 | +and use at as version number. |
12 | 11 |
|
13 | | -It this case you can get current version without any access to ``.git`` folder |
14 | | -(which is required by ``setuptools-git-versioning``). |
| 12 | +To resolve version number in runtime, you should move ``VERSION`` file to your module subfolder: |
15 | 13 |
|
16 | | -See: |
| 14 | +- ``setup.py``: |
| 15 | + |
| 16 | + Create ``MANIFEST.in`` file in the project root: |
| 17 | + |
| 18 | + .. code:: |
| 19 | +
|
| 20 | + include my_module/VERSION |
| 21 | +
|
| 22 | + Then make few changes in ``setup.py``: |
| 23 | + |
| 24 | + .. code:: python |
| 25 | +
|
| 26 | + ... |
| 27 | +
|
| 28 | + # change VERSION file path |
| 29 | + version_file = root_path / "my_module" / "VERSION" |
| 30 | +
|
| 31 | + setuptools.setup( |
| 32 | + ..., |
| 33 | + setuptools_git_versioning={ |
| 34 | + "enabled": True, |
| 35 | + "version_file": version_file, |
| 36 | + }, |
| 37 | + # read MANIFEST.in and include files mentioned here to the package |
| 38 | + include_package_data=True, |
| 39 | + # this package will read some included files in runtime, avoid installing it as .zip |
| 40 | + zip_safe=False, |
| 41 | + ) |
| 42 | +
|
| 43 | +- ``pyproject.toml``: |
| 44 | + |
| 45 | + .. code:: toml |
| 46 | +
|
| 47 | + [tool.setuptools.package-data] |
| 48 | + # include VERSION file to a package |
| 49 | + my_module = ["VERSION"] |
| 50 | + # this package will read some included files in runtime, avoid installing it as .zip |
| 51 | + zip-safe = false |
| 52 | +
|
| 53 | + [tool.setuptools-git-versioning] |
| 54 | + enabled = true |
| 55 | + # change the file path |
| 56 | + version_file = "my_module/VERSION" |
| 57 | +
|
| 58 | +And then read this file: |
| 59 | + |
| 60 | +.. code:: python |
| 61 | +
|
| 62 | + # content of my_module/__init__.py |
| 63 | +
|
| 64 | + from pathlib import Path |
| 65 | +
|
| 66 | + # you can use os.path and open() as well |
| 67 | + __version__ = Path(__file__).parent.joinpath("VERSION").read_text() |
| 68 | +
|
| 69 | +
|
| 70 | +Using ``version_callback`` option |
| 71 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +In case of using :ref:`version_callback <version-callback>` option you can directly call this callback inside a module: |
| 74 | + |
| 75 | +.. code:: python |
| 76 | +
|
| 77 | + # content of my_module/__init__.py |
| 78 | +
|
| 79 | + from my_module.version import get_version |
| 80 | +
|
| 81 | + __version__ = get_version() |
17 | 82 |
|
18 | | -* :ref:`version-file` |
19 | | -* :ref:`version-callback` |
20 | 83 |
|
21 | 84 | Using ``importlib`` |
22 | 85 | ~~~~~~~~~~~~~~~~~~~ |
@@ -66,19 +129,19 @@ Calling internals of ``setuptools_git_versioning`` module |
66 | 129 |
|
67 | 130 | .. warning:: |
68 | 131 |
|
69 | | - This way is STRONGLY DISCOURAGED. Functions in the module |
70 | | - are not a part of public API, and could be changed in the future without |
71 | | - maintaining backward compatibility. |
| 132 | + This way is STRONGLY DISCOURAGED. Functions in the module |
| 133 | + are not a part of public API, and could be changed in the future without |
| 134 | + maintaining backward compatibility. |
72 | 135 |
|
73 | 136 | .. warning:: |
74 | 137 |
|
75 | | - Use this ONLY in CI/CD tools. |
| 138 | + Use this ONLY in CI/CD tools. |
76 | 139 |
|
77 | | - NEVER use ``setuptools_git_versioning`` inside your package, because ``.git`` |
78 | | - folder is not being included into it, and target OS can lack of ``git`` executable. |
| 140 | + NEVER use ``setuptools_git_versioning`` inside your package, because ``.git`` |
| 141 | + folder is not being included into it, and target OS can lack of ``git`` executable. |
79 | 142 |
|
80 | | - ``.git`` folder and ``git`` executable presence is crucial |
81 | | - for ``setuptools-git-versioning`` to work properly. |
| 143 | + ``.git`` folder and ``git`` executable presence is crucial |
| 144 | + for ``setuptools-git-versioning`` to work properly. |
82 | 145 |
|
83 | 146 | .. code:: python |
84 | 147 |
|
|
0 commit comments