Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

5 changed files with 20 additions and 33 deletions

View File

@ -215,8 +215,8 @@ as well when you call them with a `--version' argument.
For the latter two cases, you can define a `__version__` variable in
one of the modules, or even in a dedicated module, that you then
import whereever you need it. For example, you may create a simple
module called `version.py` file with the following content:
import whereever you need it. Even better is to define it in the
`__init__.py` file:
```
__version__ = '1.4.2'
@ -226,12 +226,6 @@ __year__ = '2024'
""" Year of the current numerix version as string. """
```
In `__init__.py` you import the version:
```
from .version import __version__
```
Then a script can easily check the package's version like this:
```
@ -240,26 +234,20 @@ from numerix import __version__
print(__version__)
```
Other modules of the package may also import the version by a relative
import to be able to use it:
Other modules of the package need to import it from the `__init__.py`
file by a relative import to be able to use it:
```
from .version import __version__
from .__init__ import __version__
def about():
print(f'{__name__} {__version__}')
```
Why not defining `__version__` directly in `__init__.py`? Each module
using the version string would import it from `__init__.py`. If you
also want to import some functions from such a module in `__init__.py`
to make them more easily importable from the package, you end up in
cyclic imports. Python does not like them for a good reason...
But how do you get the very same version into the `pyproject.toml`
file? It would be a very bad idea to write it there directly and
update it whenever you change it in the `version.py` file. The
update it whenever you change it in the `__init__.py` file. The
version number should be set only in one place.
This is possible, of course. You need to specify the `version` field
@ -267,7 +255,7 @@ as `dynamic` in the `[project]` section. This tells the package tool
that the version will be set dynamically by some code. There are
several options to do so. The simplest one is to add a
`[tool.setuptools.dynamic]` section where the version is read as an
attribute from the `version` module of the `numerix` package:
attribute from the `numerix` package, that is its `__init__.py`file:
```txt
...
@ -278,7 +266,7 @@ dynamic = ["version"]
...
[tool.setuptools.dynamic]
version = {attr = "numerix.version.__version__"}
version = {attr = "numerix.__version__"}
```
## Distribute your package

View File

@ -43,7 +43,7 @@ Documentation = "https://whale.am28.uni-tuebingen.de/git/benda/packagehowto/src/
#numerix = "numerix.numerix:main"
[tool.setuptools.dynamic]
version = {attr = "numerix.version.__version__"}
version = {attr = "numerix.__version__"}
#[tool.pytest.ini_options]
#pythonpath = "src"

View File

@ -5,7 +5,16 @@ Fancy numerics made easy.
"""
from .version import __version__
__version__ = '1.4.2'
""" Current version of the numerix package as string 'x.y.z'. """
__year__ = '2024'
""" Year of the current numerix version as string. """
# Add them to documentation:
__pdoc__ = {}
__pdoc__['__version__'] = True
__pdoc__['__year__'] = True
# Functions defined directly in __init__.py can be imported directly

View File

@ -1,4 +1,4 @@
from .version import __version__
from .__init__ import __version__
from .addition import add_two

View File

@ -1,10 +0,0 @@
__version__ = '1.4.2'
""" Current version of the numerix package as string 'x.y.z'. """
__year__ = '2024'
""" Year of the current numerix version as string. """
# Add them to documentation:
__pdoc__ = {}
__pdoc__['__version__'] = True
__pdoc__['__year__'] = True