mesonbuild / meson-python

Meson PEP 517 Python build backend
https://mesonbuild.com/meson-python/
MIT License
118 stars 59 forks source link

Question: does meson-python support python namespaces #581

Closed matteoaletti closed 4 months ago

matteoaletti commented 4 months ago

I am trying to migrate a project with python namespaces from setuptools to meson.

I package it with setuptools with this option in the pyproject.toml

[tool.setuptools.packages.find] namespaces = True

While trying meson as build backend, I was able to have a working wheel only without the namespace structure. When I tried with the namespace structure I could not import the package because it was not finding the namespace.

Does meson support them?

rgommers commented 4 months ago

Hi @matteoaletti. I'm not 100% sure, but I think that using namespace packages shouldn't be a problem. We don't have a specific test for it, but if you look at https://packaging.python.org/en/latest/guides/packaging-namespace-packages/, then you'll see that all there is to it (from a package build perspective) is having a top-level directory without an __init__.py in it. And since Meson lets you explicitly control which files to install and to where, I would not expect any interesting differences between a regular and a namespace package.

I don't quite know what [tool.setuptools.packages.find] does - probably some remapping of install directories? Do you know, and can you share your code? If not, could you please try to share a minimal/dummy package that is representative of your problem?

agriyakhetarpal commented 4 months ago

I don't quite know what [tool.setuptools.packages.find] does - probably some remapping of install directories?

I can take a stab at answering this. With [tool.setuptools.packages.find], setuptools shall find namespace packages by default; with PEP517/518 the behaviour for pyproject.toml-based projects (setuptools.build_meta) is that it can emit a "Package would be ignored" warning but still includes it in the list of paths that get copied and installed subsequently. This is because include_package_data=True is enabled by default for projects with pyproject.toml-based declarative metadata, but this is disabled and set to False for setup.py-based builds or those with the setuptools.build_meta.__legacy__ build-backend (I am not aware of the reasons behind this).

find = {namespaces = false} should disable the lookup of namespace packages, though this is with [tool.setuptools.packages], not through [tool.setuptools.packages.find] (both of which just refer to the same thing, I guess, because of how TOML tables are written).

I usually end up using glob-based matching to counteract this, which is more explicit, neat, and works for both usual and namespace packages – and I would imagine Meson does, or should, behave in a similar way:

[tool.setuptools.packages.find]
where = ["myproject", "myproject.*"]  # for flat layouts

which is why it is (almost) always better to use src layouts

rgommers commented 4 months ago

Thanks for the context @agriyakhetarpal.

I'll convert this to a Discussion topic, since it's a usage question rather than a bug report. @matteoaletti if your question is answered please accept the answer in the UI. And if there's more to discuss, by all means do so.