PyO3 / maturin

Build and publish crates with pyo3, cffi and uniffi bindings as well as rust binaries as python packages
https://maturin.rs
Apache License 2.0
3.36k stars 227 forks source link

Scripts in python/project have to run `__init__.py` for --help, support including them outside directory #2047

Closed d-laub closed 1 month ago

d-laub commented 1 month ago

As far as I've tried, including scripts from inside the python/package directory slows down their help messages since they always import the package first (running everything in __init__.py). Before I had a need for Rust in my library, I could avoid this via poetry with the following layout:

project
├── src
│   ├── __init__.py
│   └── ...
├── cli.py <-- app() lives in here
├── pyproject.toml
└── README.md

The cli.py script looks like this to avoid slow imports for --help:

from typer import Typer

app = Typer()

@app.command
def main(...):
    import project
    # do stuff

if __name__ == "__main__":
    app()

And including cli:app as an entrypoint:

[tool.poetry.scripts]
project = "cli:app"

Moving to a maturin mixed project now, this breaks with the follow error:

project
├── Cargo.toml
├── python/project
│   ├── __init__.py
│   └── ...
├── cli.py <-- app() lives in here
├── pyproject.toml
├── README.md
└── src
    ├── lib.rs
    └── ...
$ project --help
Traceback...
    from cli import app
ModuleNotFoundError: No module named 'cli'

Thus, I've had to go back to including the CLI in the python/project directory and this slows down the help message responsiveness by several seconds. This is not a deal breaker, but it would be great to be able to do this. Otherwise, I'm curious if there are other workarounds to improve help message responsiveness that are compatible with maturin. Thanks!