|
| 1 | +.. _ref_build_system: |
| 2 | + |
| 3 | +############ |
| 4 | +Build System |
| 5 | +############ |
| 6 | + |
| 7 | +The build system is a fundamental tool for packaging Python |
| 8 | +libraries. It generates distribution files that can be shared with |
| 9 | +users and developers. |
| 10 | + |
| 11 | + |
| 12 | +Artifacts |
| 13 | +========= |
| 14 | + |
| 15 | +The build system allows maintainers to generate artifacts for their Python |
| 16 | +libraries. Here, `artifacts` refers to both wheel and source files: |
| 17 | + |
| 18 | +- ``Wheel files`` have the ``*.whl`` file extension. |
| 19 | +- ``Source files`` have the ``*.tar.gz`` or ``*.zip`` extension. |
| 20 | + |
| 21 | +These are the files to upload to `PyPI`_ when releasing a new version of a |
| 22 | +PyAnsys project. |
| 23 | + |
| 24 | +.. warning:: |
| 25 | + |
| 26 | + Not all files are included by default in the source distribution. A |
| 27 | + `MANIFEST.in`_ is required at the root of the project to specify additional |
| 28 | + files. |
| 29 | + |
| 30 | + |
| 31 | +The interaction between the maintainer and the build system is performed using a |
| 32 | +build system tool. This tool provides both a frontend and a backend. The maintainers |
| 33 | +trigger the frontend, which then calls the backend to read the |
| 34 | +project directory and generate the artifacts, as :numref:`build system diag` shows. |
| 35 | + |
| 36 | +.. include:: diag/build_system_diag.rst |
| 37 | + |
| 38 | + |
| 39 | +PEP 517 and PEP 518 |
| 40 | +=================== |
| 41 | + |
| 42 | +For a long time, the ``setup.py`` file was the only way of specifying the |
| 43 | +project structure, metadata, and installation workflow that `pip`_ was to follow. |
| 44 | +However, having to execute a Python file when installing a Python package |
| 45 | +introduced the following problems: |
| 46 | + |
| 47 | +- It was not possible to know which dependencies required the ``setup.py`` file |
| 48 | + to be properly executed. |
| 49 | + |
| 50 | +- The default Python package installer, `pip`_, expected `setuptools`_ to be the |
| 51 | + default build system tool, excluding others like `flit`_ and `poetry`_. |
| 52 | + |
| 53 | +These problems led to the acceptance of `PEP 517`_ and `PEP 518`_. |
| 54 | + |
| 55 | + |
| 56 | +PEP 517 |
| 57 | +------- |
| 58 | + |
| 59 | +`PEP 517`_ allows Python developers to specify the build backend tool for |
| 60 | +generating artifacts. The previous :numref:`build system diag` diagram shows the |
| 61 | +most popular backends: |
| 62 | + |
| 63 | +- `Setuptools`_ , while very popular, lacks the ability to declare build time dependencies |
| 64 | + and is difficult to extend. |
| 65 | +- `Flit`_ is a lightweight build system tool for Python. |
| 66 | +- `Poetry`_ focuses on dependency management and environment isolation. |
| 67 | + |
| 68 | +`PEP 517` introduced the ``build-backend`` key inside the |
| 69 | +``[build-system]`` table in the ``pyproject.toml``. |
| 70 | + |
| 71 | + |
| 72 | +PEP 518 |
| 73 | +------- |
| 74 | + |
| 75 | +In addition to the ``setup.py`` file, `PEP 518`_ includes a project file named |
| 76 | +``pyproject.toml``. The main goal of this file is to specify build time dependencies. |
| 77 | +However, some build system tools like `flit`_ or `poetry`_ are able to specify all |
| 78 | +project metadata inside the ``pyproject.toml`` file and eliminate usage of the |
| 79 | +``setup.py`` file. |
| 80 | + |
| 81 | +To specify the build time requirements, the ``[build-system]`` table must be |
| 82 | +declared in the ``pyproject.toml`` file. Within it, the ``requires`` key is |
| 83 | +assigned to a list of strings, which are the build time |
| 84 | +requirements. |
| 85 | + |
| 86 | +The combination of `PEP 517`_ and `PEP 518`_ leads to the following syntax in a |
| 87 | +``pyproject.toml`` file: |
| 88 | + |
| 89 | +.. code:: toml |
| 90 | +
|
| 91 | + [build-system] |
| 92 | + requires = ["flit"] # Defined by PEP 518 |
| 93 | + build-backend = "flit_core.api" # Defined by PEP 517 |
| 94 | +
|
| 95 | +
|
| 96 | +Build Backend Tools |
| 97 | +=================== |
| 98 | + |
| 99 | +This section lists some of the most popular build systems in the |
| 100 | +Python ecosystem. Although all of them achieve the same goal, there are a few |
| 101 | +differences regarding their capabilities and the way of specifying project |
| 102 | +metadata. |
| 103 | + |
| 104 | +.. TODO: Include links to each build system allowed metadata fields |
| 105 | +
|
| 106 | +Setuptools |
| 107 | +---------- |
| 108 | + |
| 109 | +`Setuptools`_ has been a part of the Python ecosystem for a long time. Unless |
| 110 | +you require high control over your project's installation steps, you should use |
| 111 | +`flit`_ or `poetry`_. |
| 112 | + |
| 113 | +If you do not need a dynamic installation process, you can consider using a |
| 114 | +``setup.cfg`` file. However, the ``setup.py`` file is still required. The ``setup.cfg`` file |
| 115 | +should have a call to the ``setup()`` function to act as the entry point of the |
| 116 | +build backend system. |
| 117 | + |
| 118 | +All of these `setuptools metadata fields`_ are valid and must be |
| 119 | +specified either in the ``setup.py`` or ``setup.cfg`` file. |
| 120 | + |
| 121 | + |
| 122 | +Flit |
| 123 | +---- |
| 124 | + |
| 125 | +Flit is a modern and lightweight build system that requires developers |
| 126 | +to manage virtual environments on their own. Developers must: |
| 127 | + |
| 128 | +* Create a virtual environment and activate it. |
| 129 | +* Install the package in editable mode. |
| 130 | + |
| 131 | +Flit is the default tool for creating a new ``pyansys`` project when using the |
| 132 | +`ansys-templates tool`_. |
| 133 | + |
| 134 | +The ``[project]`` section specifies the project's metadata and required |
| 135 | +dependencies. For more information, see `flit pyproject.toml |
| 136 | +guidelines`_. |
| 137 | + |
| 138 | + |
| 139 | +Poetry |
| 140 | +------ |
| 141 | + |
| 142 | +Because of its ``poetry.lock`` file, Poetry provides strong dependency pinning. When |
| 143 | +installing a package, poetry creates a virtual environment, thus ensuring an isolated |
| 144 | +package development environment. |
| 145 | + |
| 146 | +Nevertheless, it is possible to make Poetry ignore the ``poetry.lock`` file with: |
| 147 | + |
| 148 | +.. code:: bash |
| 149 | +
|
| 150 | + poetry config virtualenvs.create false --local |
| 151 | +
|
| 152 | +Using `poetry`_ is popular because it: |
| 153 | + |
| 154 | +* Supports pinning dependency versions via a ``poetry.lock`` file that can be |
| 155 | + used for testing and CI |
| 156 | +* Allows downstream packages to still consume a loose dependency specification |
| 157 | +* Integrates with `dependabot`_ to update the pinned version |
| 158 | + |
| 159 | +The ``[tool.poetry]`` section contains metadata and defines project |
| 160 | +dependencies. For more information, see `poetry pyproject.toml documentation`_. |
| 161 | + |
| 162 | + |
| 163 | +.. _MANIFEST.in: https://packaging.python.org/en/latest/guides/using-manifest-in/ |
| 164 | +.. _PyPI: https://pypi.org/ |
| 165 | +.. _pip: |
| 166 | +.. _flit: https://flit.pypa.io/en/latest/ |
| 167 | +.. _poetry: https://python-poetry.org/ |
| 168 | +.. _poetry pyproject.toml documentation: https://python-poetry.org/docs/pyproject/ |
| 169 | +.. _setuptools: https://pypi.org/project/setuptools/ |
| 170 | +.. _setuptools metadata fields: https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#declarative-config |
| 171 | +.. _flit pyproject.toml guidelines: https://flit.readthedocs.io/en/latest/pyproject_toml.html |
| 172 | +.. _dependabot: https://docs.github.com/en/code-security/supply-chain-security/keeping-your-dependencies-updated-automatically/about-dependabot-version-updates |
| 173 | +.. _ansys-templates tool: https://github.com/pyansys/ansys-templates |
0 commit comments