3

My current directory is set up like this

src
  api
    __init__.py
    class1.py
    py.typed
  models
    __init__.py
    class2.py
    py.typed
tests
  test_class_1.py
  test_class_2.py
requirements.txt
requirements_dev.txt
pyproject.toml
setup.cfg
setup.py
tox.ini

I have made models an installable package so that I can import it in my tests directory for unit testing. I would also like to make api an installable package so that I can do the same thing.

Is there a way to do this?

Can you use the same pyproject.toml and setup.cfg files? Do you need to have separate versions of those files for each installable package you want to create?

  • Please have a look at [`bit_demo`](https://codeberg.org/buhtz/bit_demo) as a demonstrator. Also see [this answer](https://stackoverflow.com/a/76315388/4865723) by me. It is not clear what you want: Tell use how do you would use `pip install` and how do you do `import x.y.z` in your case. – buhtz May 26 '23 at 18:41
  • 1
    You can have two top-level importable packages in the same distribution package. It is unconventional (as in: it is kind of annoying and frowned upon in the Python packaging ecosystem), but it is technically feasible. And of course if you have no intent to make this public, then this is not much of a worry. -- ([reference](https://sinoroc.gitlab.io/kb/python/packaging.html) I wrote for myself on the terminology) – sinoroc May 26 '23 at 18:57
  • @sinoroc Your source/URL is not wrong but out-dated. You still use `setup.py` in there. No need for that anymore today. It is possible to reach what @DuncanCarlmark asked for with `pyproject.toml` only. – buhtz May 26 '23 at 19:04
  • 1
    @buhtz That page mentions `setup.py` only once for something completely unrelated to the question here. But yes, coincidentally the paragraph containing the `setup.py` reference has no relevance anymore, not for the reasons you mention (in other words it has not become irrelevant because of the apparition of `pyproject.toml`) but because no one uses anything else but `.tar.gz` for source distributions. – sinoroc May 26 '23 at 19:20
  • 1
    @buhtz For info: the part with `setup.py` is now removed. – sinoroc May 27 '23 at 08:13

1 Answers1

1

Just one file is required. It is fine to have two or more top-level packages contained in the same distribution. In pyproject.toml (no setup.cfg/setup.py is required):

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.setuptools.package-dir]
"" = "src"

[tool.setuptools]
packages = ["api", "models"]
wim
  • 338,267
  • 99
  • 616
  • 750
  • `"" = "src"` seems odd. Can you explain this and/or link to the relevant documentation? – Socowi May 30 '23 at 02:39
  • 1
    It's passed on to distutils and is documented there https://docs.python.org/3/distutils/setupscript.html#listing-whole-packages – wim May 30 '23 at 02:44
  • [This answer](https://stackoverflow.com/a/71668966/674039) shows what might be a more familiar `setup.py` syntax for the same thing. – wim May 30 '23 at 21:02