sklearn-like imports / Hiding from public API

21 views Asked by At

Suppose you have a Python package with a src-layout as follows:

package_folder
...
project.toml
/src
  /package
    __init__.py
    /models
      __init__.py
      _model_1.py
    ...

The file _model_1.py contains a class Model1. When package is installed, I'd like to import Model1asfrom package.models import Model1. For instance, in sklearn you import MDSasfrom sklearn.manifold import MDSalthough MDS is a class insklearn/manifold/_mds.py. I specified the class to be imported with __all__ as in here:

## /src/package/models/__init__.py
from ._model_1 import Model1
__all__ = ["Model1"]

However, it does not work as expected, as the only way it works is by importing as from package.moldels._model_1 import Model1. I am trying to use only setuptools TOML. How can I make this work?

Thanks in advance!

This is the current project.toml I am using:

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

[project]
name = "package"
version = "0.1"

[tool.setuptools.packages.find]
where = ["src"]
1

There are 1 answers

0
Mark Petersen On

Deleting

[tool.setuptools.packages.find]
where = ["src"]

and adding

[tool.setuptools]
package_dir = {"": "src"}

solved the problem. (why?)