• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _docs-style-python:
2
3==================
4Python style guide
5==================
6.. _Python: https://www.python.org/
7
8Pigweed uses the standard `Python`_ style: `PEP8
9<https://www.python.org/dev/peps/pep-0008/>`__. All Pigweed Python code should
10pass ``pw format``, which invokes ``black`` with a couple options.
11
12---------------
13Python versions
14---------------
15Pigweed officially supports :ref:`a few Python versions
16<docs-concepts-python-version>`. Upstream Pigweed code must support those Python
17versions. The only exception is :ref:`module-pw_env_setup`, which must also
18support Python 2 and 3.6.
19
20.. _docs-style-python-extend-generated-import-paths:
21
22-------------------------------------------------------
23Extend the import path of packages with generated files
24-------------------------------------------------------
25Python packages that include generated files should extend their import path
26using the following snippet at the beginning of their ``__init__.py``:
27
28.. code-block:: python
29
30   # This Python package contains generated Python modules that overlap with
31   # this `__init__.py` file's import namespace, so this package's import path
32   # must be extended for the generated modules to be discoverable.
33   #
34   # Note: This needs to be done in every nested `__init__.py` that will contain
35   # overlapping generated files.
36   __path__ = __import__('pkgutil').extend_path(__path__, __name__)
37