@@ -20,7 +20,7 @@ what Briefcase is doing). The steps required are documented in the CPython usage
20
20
guides:
21
21
22
22
* [ macOS] ( https://docs.python.org/3/using/mac.html )
23
- * [ iOS] ( https://docs.python.org/3.14 /using/ios.html )
23
+ * [ iOS] ( https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project )
24
24
25
25
For tvOS and watchOS, you should be able to broadly follow the instructions in
26
26
the iOS guide.
@@ -29,7 +29,7 @@ the iOS guide.
29
29
30
30
There are 2 ways to access the Python runtime in your project code.
31
31
32
- ### Embedded C API.
32
+ ### Embedded C API
33
33
34
34
You can use the [ Python Embedded C
35
35
API] ( https://docs.python.org/3/extending/embedding.html ) to instantiate a Python
@@ -43,37 +43,31 @@ An alternate approach is to use
43
43
[ PythonKit] ( https://github.com/pvieito/PythonKit ) . PythonKit is a package that
44
44
provides a Swift API to running Python code.
45
45
46
- To use PythonKit in your project:
46
+ To use PythonKit in your project, add the Python Apple Support package to your
47
+ project as described above; then:
47
48
48
49
1 . Add PythonKit to your project using the Swift Package manager. See the
49
50
PythonKit documentation for details.
50
51
51
- 2 . Create a file called ` module.modulemap ` inside
52
- ` Python.xcframework/macos-arm64_x86_64/Headers/ ` , containing the following
53
- code:
54
- ```
55
- module Python {
56
- umbrella header "Python.h"
57
- export *
58
- link "Python"
59
- }
60
- ```
61
-
62
- 3 . In your Swift code, initialize the Python runtime. This should generally be
52
+ 2 . In your Swift code, initialize the Python runtime. This should generally be
63
53
done as early as possible in the application's lifecycle, but definitely
64
- needs to be done before you invoke Python code:
54
+ needs to be done before you invoke Python code. References to a specific
55
+ Python version should reflect the version of Python you are using:
65
56
``` swift
66
57
import Python
67
58
68
- guard let stdLibPath = Bundle.main.path (forResource : " python-stdlib" , ofType : nil ) else { return }
69
- guard let libDynloadPath = Bundle.main.path (forResource : " python-stdlib/lib-dynload" , ofType : nil ) else { return }
70
- setenv (" PYTHONHOME" , stdLibPath, 1 )
71
- setenv (" PYTHONPATH" , " \( stdLibPath ) :\( libDynloadPath ) " , 1 )
59
+ guard let pythonHome = Bundle.main.path (forResource : " python" , ofType : nil ) else { return }
60
+ guard let pythonPath = Bundle.main.path (forResource : " python/lib/python3.13" , ofType : nil ) else { return }
61
+ guard let libDynloadPath = Bundle.main.path (forResource : " python/lib/python3.13/lib-dynload" , ofType : nil ) else { return }
62
+ let appPath = Bundle.main .path (forResource : " app" , ofType : nil )
63
+
64
+ setenv (" PYTHONHOME" , pythonHome, 1 )
65
+ setenv (" PYTHONPATH" , [pythonPath, libDynloadPath, appPath].compactMap { $0 }.joined (separator : " :" ), 1 )
72
66
Py_Initialize ()
73
67
// we now have a Python interpreter ready to be used
74
68
```
75
69
76
- 5 . Invoke Python code in your app. For example:
70
+ 3 . Invoke Python code in your app. For example:
77
71
``` swift
78
72
import PythonKit
79
73
0 commit comments