Use base executable when it exists, to avoid recompiling when switching venv#429
Conversation
|
Thanks for the PR! This diff seems very reasonable to me, and definitely a desirable goal to reduce recompilation. A couple of design questions:
So overall this seems reasonable to me. We should add a note in the CHANGELOG for this please. |
My assumption is that since PyO3 (in this case) cares about Python from a compilation POV, virtual environments don't affect it as all the interesting header-related things are not touched by the venv. From what I read online, the issues other users were having with PyO3 was to do with activating a venv when running Python embedded in Rust, so this would have no effect (good or bad) on that. But my search was not very thougher, are there any other known issues?
We could, my worry would be that there are a lot of special cases, since I don't think there's a requirement that the name of the executable matches (that seems to have been the problem with python/cpython#99204). By using
Will do! |
|
This does not seem like a reliable solution, I've tried in maturin, when using with poetry to install an editable pyo3 based dep, $ /tmp/tmp3uzvm48r/.venv/bin/python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/tmp/tmp3uzvm48r/.venv/bin/python'
>>> sys._base_executable
'/tmp/tmp3uzvm48r/.venv/bin/python'the isolated venv poetry creates has Edit: seems that we just need to use |
When using a requirements file compilation tool like pip-tools, a lot of temporary virtual environments are created, into which packages are installed. When a package uses setuptools-rust, the value of
sys.executablekeeps changing, which cause cargo to recompile quite a few crates (pyo3 and anything that depends on it). This slows things down quite a lot.This PR uses the semi-documented
sys._base_executablevalue, which points to the real Python interpreter when inside a venv. If it doesn't exist, or the provided path is not valid, we fall back to the current behaviour.I tested this by running a requirements compilation, and checking for rustc invocations: There are a lot before this change, and none after (provided the rust package in question was already compiled).