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

Setting PYTHONHOME when initialising Python does not work as intended #19

Open
samschott opened this issue Nov 27, 2022 · 2 comments
Open
Labels
bug A crash or error in behavior.

Comments

@samschott
Copy link
Member

Describe the bug
The main.m program which initialises the Python runtime sets PYTHONHOME here:

python_home = [NSString stringWithFormat:@"%@/support/python-stdlib", resourcePath, nil];
NSLog(@"PythonHome: %@", python_home);
wtmp_str = Py_DecodeLocale([python_home UTF8String], NULL);
status = PyConfig_SetString(&config, &config.home, wtmp_str);

This is does not work as intended. As documented in https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME, the home is suffixed with /lib/python3.11, resulting in the Python runtime believing that

Contents/Resources/support/python-stdlib/lib/python3.11

is the location of its stdlib.

In practice, this is not a problem because we later explicitly include the stdlib in the Python path:

path = [NSString stringWithFormat:@"%@/support/python-stdlib/lib-dynload", resourcePath, nil];
NSLog(@"- %@", path);
wtmp_str = Py_DecodeLocale([path UTF8String], NULL);
status = PyWideStringList_Append(&config.module_search_paths, wtmp_str);

This does mean however that the initially quoted lines currently are not useful at all.

To Reproduce

  1. Build the Xcode project.
  2. From the Python program, inspect ``.

Additional context
We could do either of the following:

  1. Completely rely on the PYTHONPATH to contain the stdlib.
  2. Set the stdlib dir correctly, if possible, and omit it the PYTHONPATH.
  3. Change the location of the stdlib directory to Contents/Resources/support/lib/python3.11
@samschott samschott added the bug A crash or error in behavior. label Nov 27, 2022
@freakboy3742
Copy link
Member

Well that's... annoying. Can't argue with the logic, but it's going to require a bunch of changes to accomodate it - the support package will need to be updated to produce the "lib/python3.11" rather than "python-stlib" location. The good news is that the recent change to Briefcase version-based branching and support package version pinning means we can introduce this change without breaking anything.

I'd say option (3) is probably our best option; if nothing else, it would allow for other code to be added to the lib folder. In the interests of avoiding hideously deep directory structures, it might also be worth exploring whether the support subfolder is needed, or whether we could use lib as the support folder.

@samschott
Copy link
Member Author

I agree, Contents/Resources/lib/python3.11 or equivalent paths on other platforms would work just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior.
Projects
None yet
Development

No branches or pull requests

2 participants