Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running Maturin through Python #1007

Closed
BD103 opened this issue Jul 16, 2022 · 1 comment · Fixed by #1008
Closed

Allow running Maturin through Python #1007

BD103 opened this issue Jul 16, 2022 · 1 comment · Fixed by #1008
Labels
enhancement New feature or request

Comments

@BD103
Copy link

BD103 commented Jul 16, 2022

Reasoning

Python has support for running the __main__.py file of a package through the python -m <pkg> command. I noticed that while installing Maturin exposes the maturin command, it does not allow you to run python -m maturin.

One of the reasons for allowing this method is to choose which specific Python interpreter to use. I believe that using specific versions of Python is necessary for certain bindings. (I'm not positive on that, though.) If a developer has multiple versions of Maturin installed on there machine, each for a different Python version, this allows them to gracefully pick which version to use.

# Instead of
$ /path/to/python3.8/bin/maturin develop
# We could run
$ python3.8 -m maturin develop

Or for windows:

$ py -3.9 -m maturin develop

Another thing to keep in mind is that multiple popular packages use __main__.py as well. PIP, Flask, Rich, and many others use it.

Solution

I propose that Maturin exposes a __main__.py file in the maturin folder. This file would either call the executable file directly, or call the specific function in the Rust bindings.

Drawbacks

While doing this would be good for compatibility, there are a few issues. The first is that the only way the Python package can access Maturin is through calling the executable with subprocess. This makes assumptions that Maturin is on the path already. If this method is used, there is no way to call a specific Python interpreter. This essentially removes the point of using __main__ in the first place.

The fix this, __main__ needs to figure out where the specific version's Python folder is, then call the located executable. This isn't that difficult to do, (I'm pretty sure there's a built-in function for finding the folder), but still seems overly-complicated.

On the other hand, if we didn't want to use subprocess, we'd have to ship a library file with the folder. We'd also have to abstract main.rs to another file that can be imported by Python. This version probably won't work, as it requires too many breaking changes.

Conclusion

Adding a __main__.py file to the Python package that can be executed with python -m maturin would benefit both compatibility and version-specification. This Python file would locate where binaries are installed for that specific Python version (or virtual environment), then call the Maturin executable that exists there.

Thank you for your time and consideration,
~ BD103

@BD103
Copy link
Author

BD103 commented Jul 17, 2022

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant