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

Update Usage.md #234

Open
olilarkin opened this issue Sep 30, 2024 · 7 comments
Open

Update Usage.md #234

olilarkin opened this issue Sep 30, 2024 · 7 comments
Labels
enhancement New features, or improvements to existing features.

Comments

@olilarkin
Copy link

What is the problem or limitation you are having?

It seems that maybe a recent switch to a different package layout with the xcframework has made the Usage.md instructions obsolete? I was able to use PythonKit without the module map but it is unclear to me now how i install dependencies since the xcframework contains the site packages folder.

Describe the solution you'd like

update about how it's meant to work

Describe alternatives you've considered

ditto

Additional context

thanks for this cool project!

@olilarkin olilarkin added the enhancement New features, or improvements to existing features. label Sep 30, 2024
@freakboy3742
Copy link
Member

Support for PythonKit is provided on a "best effort" basis; we're happy to incorporate changes to documentation, but we're not actively monitoring for changes.

FWIW, the Briefcase iOS template gives the paths that are used by the new project layout:

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *python_home = [NSString stringWithFormat:@"%@/python", resourcePath, nil];
NSString *stdlib_path = [NSString stringWithFormat:@"%@/lib/python3.13", python_home, nil];
NSString *libdynload_path = [NSString stringWithFormat:@"%@/lib/python3.13/lib-dynload", python_home, nil];

I think that is all the detail you need to adapt the current usage guide; if you want to convert that into a PR updating USAGE.md, I'll happily merge those changes.

@kostrykin
Copy link

kostrykin commented Oct 20, 2024

I'm following a guide on how to embed a Python interpreter into a Swift app for macOS, and it tells to copy the python-stdlib directory from a release of this repo into the project directory. The guide also tells to ensure that python-stdlib is contained in "Copy Bundle Resources" and all .so files from within python-stdlib are signed.

I downloaded a few releases from this repo, for example https://github.com/beeware/Python-Apple-support/releases/tag/3.11-b5, but there isn't such a directory. Do you maybe have a suggestion how to proceed?


Update: I have added the Python.xcframework from the release to the Xcode project but when I try import Python all I get is No such module 'Python'. What am I missing?

I was able to use PythonKit without the module map but it is unclear to me now how i install dependencies since the xcframework contains the site packages folder.

@olilarkin Can you maybe elaborate on which steps exactly you took to make it work?

@freakboy3742
Copy link
Member

3.11-b4 (and other version releases at the same time) changed the structure of the support package to use the binary format and loading mechanism that was approved as part of PEP 730. This required some changes to the layout of the package.

Any errors associated with "No such module Python", or any difficulties loading the standard library can almost certainly be traced back to the initialisation of the module (and in particular paths) being correct.

As noted above, the usage guide for PythonKit is provided on a best-effort basis; if you want to see what is required to make the support package work, the project generated by Briefcase is the canonical reference implementation.

@kostrykin
Copy link

Thanks for the reply!

Any errors associated with "No such module Python", or any difficulties loading the standard library can almost certainly be traced back to the initialisation of the module (and in particular paths) being correct.

Maybe I'm misunderstanding something here, because from what I've read, I assumed that the initialization of the module happens after the import Python instruction (for example, see Step 3 in https://github.com/beeware/Python-Apple-support/blob/main/USAGE.md#pythonkit). Is there anything that I need to put before the import Python instruction?

As noted above, the usage guide for PythonKit is provided on a best-effort basis;

Yes I understand that. I appreciate this very much.

if you want to see what is required to make the support package work, the project generated by Briefcase is the canonical reference implementation.

Thanks for the pointer, I have tried setting up a briefcase project using the macOS Xcode template as described in the README file (pip install cookiecutter, btw the step pip install briefcase is missing, cookiecutter https://github.com/beeware/briefcase-macOS-Xcode-template, then I put the extracted 3.11-b4 release into the Support directory). This gives me an Xcode project to work with. However, when I create a Swift file inside this project and add the line import Python, I still get the No such module 'Python' error.

@freakboy3742
Copy link
Member

Any errors associated with "No such module Python", or any difficulties loading the standard library can almost certainly be traced back to the initialisation of the module (and in particular paths) being correct.

Maybe I'm misunderstanding something here, because from what I've read, I assumed that the initialization of the module happens after the import Python instruction (for example, see Step 3 in https://github.com/beeware/Python-Apple-support/blob/main/USAGE.md#pythonkit). Is there anything that I need to put before the import Python instruction?

I know almost nothing about PythonKit, so I can't say for certain - but as far as I know there's nothing needed in code before you invoke import Python.

However, your Xcode project needs to include Python correctly, and ensure that it is copied into the project as a target. A failure to load a module likely indicates that the framework isn't being copied into the project correctly, or there's some other (swift-specific) module configuration that needs to be performed (such as configuring a loading/linking path).

@kostrykin
Copy link

It works basically out of the box using the old release layout (e.g., 3.11-b3) following the steps described here.

Following basically the same steps for releases with the new layout (e.g., 3.11-b4) however does not work and yields the No such module 'Python' error. I would be very grateful if @olilarkin could describe the steps they took to make it work with the new layout :)

@olilarkin
Copy link
Author

olilarkin commented Oct 23, 2024

I've made a template repo for a macOS based on the info here

https://github.com/olilarkin/SwiftPythonTemplate

I hope that helps

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

No branches or pull requests

3 participants