-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Use PyConfig_InitPythonConfig instead of PyConfig_InitIsolatedConfig #4473
Conversation
Hi @danielcjacobs, are you aware of #4119 already? Super-over-simplified: there was a lot of back and forth, ultimately I was going by advice from @vstinner |
Lots of failures. When the CI is finished, there should be a "Download log archive" option under the top-right gear here: https://github.com/pybind/pybind11/actions/runs/4025749264/jobs/6919506867 I recommend using that to get the zip file, unpack, systematically analyze what's failing. (You only need to inspect *.txt files at the top level. You can ignore subdirs, those only have redundant info AFAIK.) At looks like mostly >=3.9 Linux & macOS are failing. Windows seems to be fine. I'd spend 10 minutes to be sure exactly what platforms are and are not affected, before continuing experimenting. |
@rwgk is there a simple way to run tests locally? |
6780e44
to
c9357c0
Compare
See .github/workflows/ci.yml. They main option is
To see the commands emitted:
|
c9357c0
to
2c89944
Compare
c518c50
to
7a67308
Compare
Wow ... this works, really? :-) LGTM |
@vstinner is there a chance that you could look at this tiny change? What do you think? It boils down to just this: - PyConfig_InitIsolatedConfig(&config);
- config.isolated = 0;
- config.use_environment = 1;
+ PyConfig_InitPythonConfig(&config);
+ config.parse_argv = 0; |
7a67308
to
b91d98e
Compare
@rwgk @vstinner OH I believe I figured out why. The docs on parse_argv say:
This sentence confused me for quite a while, but after playing around, it just seems to mean if the value is 1, it will do what "regular python" does and strip the first argument (which it expects to just be I added a unit test in 699aee5 to demonstrate this behavior. |
211694d
to
699aee5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Thoughts @rwgk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestions are more-or-less just nits. The core change seems much more intuitive to me, too.
I have two concerns that are more higher-level:
-
Ideally addressed in this PR, if we can find a reasonably practical way to implement: a new unit test exercising the original
matplotlib
in~/.local/python3.11/site-packages
situation. Is my understanding correct that we still don't have that test, even with this PR as it is right now? -
Definitely as a separate project: While looking around this doubt grew bigger: are we sure we really have it right now?
In particular:
-
How are "regular" Python command line options (https://docs.python.org/3/using/cmdline.html#using-on-cmdline) processed in the two code branches for
PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
false/true? Is the behavior actually consistent between the two? -
Highly speculative: could it be that the default
parse_argv = 1
is actually better if we make adjustments elsewhere in embed.h? Specifically, could that help us eliminating this very kludgy looking code:
pybind11/include/pybind11/embed.h
Lines 164 to 167 in a500f43
PyRun_SimpleString("import sys, os.path; " | |
"sys.path.insert(0, " | |
"os.path.abspath(os.path.dirname(sys.argv[0])) " | |
"if sys.argv and os.path.exists(sys.argv[0]) else '')"); |
20b138d
to
7fdcd00
Compare
Correct, I haven't added a test for this. To test it, we'd have to install a package to the user site directory, and try to import it in the test. I'm not sure the how to do the first part.
I am thinking if we're not running in an isolated environment, then we wouldn't need to append the path of the running file (regardless of what I also don't know if it would be valid to set |
Weird flake on pre-commit CI, local pre-commit work so it looks good. |
I've never seen that error before. I'll retry the CI via close-reopen here. The changes look good to me. |
Messing with the environment will be tricky indeed. I'm not happy to let this slip, but I'm ok with it, for practical reasons. We're not making anything worse. |
It's not a flake unfortunately: home-assistant/core#86892 @henryiii what could we do about this? Especially in light of this comment: home-assistant/core#86892 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's wait a day or so to see if @henryiii can help us with the pre-commit issue right here, for simplicity. If not, we can merge this PR and open another one to fix the issue separately.
If it's the isort issue, just bump isort. Or we can move (and combine our other python linters) to Ruff. I thought the error was different. |
Trying here: #4480 Thanks! |
7fdcd00
to
5574187
Compare
Depending on the pybind11 version, this works - or doesn't. There's a long back story here: pybind/pybind11#4473
Description
Attempts to address #4471
initialize_interpreter
initializes an isolated configuration viaPyConfig_InitIsolatedConfig
. The docs on this config say:After initializing this config, the function then sets
config.isolated
to false,config.use_environment
to true, andconfig.install_signal_handlers
to the parameter passed toinitialize_interpreter
, which undoes most of the characteristics that make this an isolated config.The isolated configuration also says it ignores command line arguments, yet we pass them along anyway.
On top of that, this configuration ignores the user site directory, which means the interpreter won't be able to find any modules installed there (
matplotlib
is an example of a module that usually defaults to the user site directory)It seems that
PyConfig_InitPythonConfig
makes much more sense here, as the docs for this configuration say:This seems much more consistent with what we actually want.
Note:
I can see this was brought up in the discussion for #4119, specifically here, though I'm a bit confused as to what the reasoning was behind sticking with the isolated config. Seems like maybe it was just that a unit test was failing. If that test still fails with this MR I may have time to play around and figure out why.
UPDATE:
After some digging, it seems that the necessary change that was missing in #4119 is setting
config.parse_argv
to 0, which is the value used for the isolated configuration. If we just set this to 0,PyConfig_InitPythonConfig
seems to work.Suggested changelog entry: