Skip to content

Commit 65a87bb

Browse files
greschdjorgepiloto
authored andcommitted
Update packaging guide to refer to 'flit'
1 parent 3567320 commit 65a87bb

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

doc/source/library_description/packaging.rst

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ Namespace Packaging
1111
-------------------
1212
A PyAnsys library uses `namespace packaging`_.
1313
Namespace packages allow a user to easily split subpackages from a package into
14-
a single, independent distribution.
14+
single, independent distributions.
1515

16-
Three different approaches are currently available for creating a namespace package:
17-
18-
* `native namespace packages`_
19-
* pkgutil-style namespace packages
20-
* pkg_resources-style namespace packages
16+
There are different approaches available for creating a namespace package. For the
17+
``ansys`` namespace, we use the `PEP 420`_ `native namespace packages`_ approach.
2118

2219
Required Files
2320
--------------
@@ -27,48 +24,71 @@ Required Files
2724

2825
* LICENSE file: Specifies copyrights and required authorization.
2926

30-
* setup.py file: Provides package information.
31-
The presence of this file indicate that the package was likely created using ``disutils``,
32-
which is the Python standard for building and distributing a Python package.
27+
* pyproject.toml file: Provides package information.
28+
This file provides the package metadata, and defines how it is built.
29+
There are different build backends available, such as `setuptools`_,
30+
`poetry`_ and `flit`_.
3331

3432

35-
Setup File
36-
----------
37-
The `setup.py`_ file is the build script for ``setuptools``. It exposes dynamic metadata and contains
38-
package information, such as a description, author, and version.
39-
In this file, the ``setuptools`` module is used to configure the metadata (as opposed to ``distutils``).
33+
Project Configuration File
34+
--------------------------
4035

41-
.. code:: python
36+
The ``pyproject.toml`` file is the standardized build configuration file for Python
37+
projects. It needs to at least contain a ``[build-system]`` section, which determines
38+
how the project is built. Some commonly used packaging tools are `setuptools`_,
39+
`poetry`_, or `flit`_.
40+
41+
When writing a *library*, `flit`_ is a good default choice. For *applications*,
42+
`poetry`_ is a good default as it provides features to pin dependency versions.
43+
We use `flit`_ in the template repository and the description below.
44+
45+
To use `flit`_ as a packaging tool, the ``pyproject.toml`` should contain
46+
47+
.. code:: toml
4248
43-
import setuptools
44-
setuptools.setup(...)
49+
[build-system]
50+
requires = ["flit_core >=3.2,<4"]
51+
build-backend = "flit_core.buildapi"
4552
46-
This file gathers all namespace packages and files that must be included in the distributed
47-
package.
53+
The ``[project]`` section contains metadata, and defines the project's dependencies. Refer to the
54+
`flit metadata documentation`_ for details.
55+
56+
Flit can automatically determine the project's version from the source code.
57+
In the ``[project]`` section, add
58+
59+
.. code:: toml
60+
61+
dynamic = ["version"]
62+
63+
The version is parsed from the ``ansys/package/library/__init__.py`` file, which must
64+
contain a statement
4865

4966
.. code:: python
5067
51-
packages = []
52-
for package in setuptools.find_namespace_packages(include='ansys*'):
53-
if package.startswith('ansys.tools.example_coverage'):
54-
packages.append(package)
68+
__version__ = "0.1.0"
69+
70+
Where supported, we aim to put all tooling-related configuration into ``pyproject.toml``.
71+
For example, it can also be used to configure the code formatter `black`_ or the static
72+
type checker `mypy`_.
5573

74+
.. note::
5675

57-
It also extracts the version number from the ``_version.py`` file located in the
58-
``ansys/<product>/library`` directory of the source code.
76+
When using `setuptools`_ as a build backend, providing the metadata in ``pyproject.toml`` is not yet fully supported.
77+
Instead, it also requires a ``setup.cfg`` and / or ``setup.py`` file.
5978

6079

6180
Generate the Package and Upload It on PyPI
6281
------------------------------------------
6382

64-
The first time that you want to upload a package on PyPI under the `ansys <https://pypi.org/user/ansys/>`_
83+
The first time that you want to upload a package on PyPI under the `ansys <https://pypi.org/user/ansys/>`_
6584
account, you must perform the following process manually.
6685

6786
Create the python package.
6887

6988
.. code::
7089
71-
python setup.py sdist
90+
pip install build
91+
python -m build
7292
7393
Verify the distribution's long description rendering with ``twine``.
7494

@@ -77,7 +97,7 @@ Verify the distribution's long description rendering with ``twine``.
7797
pip install twine
7898
twine check dist/*
7999
80-
Upload the package to PyPI using ``twine`` and the upload token generated for the ``ansys`` PyPI account.
100+
Upload the package to PyPI using ``twine`` and the upload token generated for the ``ansys`` PyPI account.
81101
Contact [email protected] for the token.
82102

83103
.. code::
@@ -127,7 +147,7 @@ Install a package with:
127147

128148
.. code::
129149
130-
pip install ansys.<product>.<library>
150+
pip install ansys-<product>-<library>
131151
132152
To create a package complying with the above standards, here is the minimal content of your PyAnsys library:
133153

@@ -136,12 +156,19 @@ To create a package complying with the above standards, here is the minimal cont
136156
ansys/<product>/<library>/__init__.py
137157
LICENSE
138158
README.rst
139-
setup.py
159+
pyproject.toml
140160
tests/
141161
142162
143163
.. _namespace packaging: https://packaging.python.org/guides/packaging-namespace-packages/
144164
.. _native namespace packages: https://packaging.python.org/guides/packaging-namespace-packages/#native-namespace-packages
165+
.. _PEP 420: https://www.python.org/dev/peps/pep-0420/
166+
.. _setuptools: https://setuptools.pypa.io
167+
.. _poetry: https://python-poetry.org/docs/
168+
.. _flit: https://flit.readthedocs.io
169+
.. _flit metadata documentation: https://flit.readthedocs.io/en/latest/pyproject_toml.html#new-style-metadata
170+
.. _black: https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
171+
.. _mypy: https://mypy.readthedocs.io/en/stable/config_file.html#the-mypy-configuration-file
145172
.. _trunk-based development: https://trunkbaseddevelopment.com/
146173
.. _secret: https://docs.github.com/en/actions/reference/encrypted-secrets
147174
.. _setup.py: https://packaging.python.org/tutorials/packaging-projects/#configuring-metadata

0 commit comments

Comments
 (0)