From 448f4610d5a7f4d7b405b5db2d71cc0aefd20e7b Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:09:57 -0800 Subject: [PATCH 01/20] Update USAGE.md for iOS and macOS --- USAGE.md | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/USAGE.md b/USAGE.md index 12bc1dc7..396d2e84 100644 --- a/USAGE.md +++ b/USAGE.md @@ -49,8 +49,10 @@ To use PythonKit in your project: PythonKit documentation for details. 2. Create a file called `module.modulemap` inside - `Python.xcframework/macos-arm64_x86_64/Headers/`, containing the following - code: + - `Python.xcframework/macos-arm64_x86_64/Python.framework/Versions/3.13/include/python3.13/` (macOS Device), + - `Python.xcframework/ios-arm64_x86_64-simulator/Python.framework/Headers/` (iOS Simulator), + - `Python.xcframework/ios-arm64/Python.framework/Headers/` (iOS Device), + containing the following code: ``` module Python { umbrella header "Python.h" @@ -59,21 +61,47 @@ module Python { } ``` -3. In your Swift code, initialize the Python runtime. This should generally be - done as early as possible in the application's lifecycle, but definitely - needs to be done before you invoke Python code: +3. In the “Build Settings” tab, modify the following: +- Build Options + - User Script Sandboxing: No + - Enable Testability: Yes +- Search Paths + - Framework Search Paths: $(PROJECT_DIR) + - Header Search Paths: "$(BUILT_PRODUCTS_DIR)/Python.framework/Headers" +- Apple Clang - Warnings - All languages + - Quoted Include In Framework Header: No + +4. In your Swift code, set environment variables when init python (iOS only, macOS do not need this step) + ```swift import Python -guard let stdLibPath = Bundle.main.path(forResource: "python-stdlib", ofType: nil) else { return } -guard let libDynloadPath = Bundle.main.path(forResource: "python-stdlib/lib-dynload", ofType: nil) else { return } -setenv("PYTHONHOME", stdLibPath, 1) -setenv("PYTHONPATH", "\(stdLibPath):\(libDynloadPath)", 1) +guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } +setenv("PYTHONHOME", pythonHome, 1) + +/* + The PYTHONPATH for the interpreter includes: + the python/lib/python3.X subfolder of your app’s bundle, + the python/lib/python3.X/lib-dynload subfolder of your app’s bundle, and + the app subfolder of your app’s bundle +*/ +guard let pythonPath = Bundle.main.path(forResource: "python/lib/python3.13", ofType: nil) else { return } +guard let libDynLoad = Bundle.main.path(forResource: "python/lib/python3.13/lib-dynload", ofType: nil) else { return } +let appPath = Bundle.main.path(forResource: "app", ofType: nil) +setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) +``` + + +5. In your Swift code, initialize the Python runtime. This should generally be + done as early as possible in the application's lifecycle, but definitely + needs to be done before you invoke Python code: +``` +import Python Py_Initialize() // we now have a Python interpreter ready to be used ``` -5. Invoke Python code in your app. For example: +6. Invoke Python code in your app. For example: ```swift import PythonKit From 5aca45d2e799558a9d1534fb29836d8e470572cd Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:17:07 -0800 Subject: [PATCH 02/20] Update USAGE.md for Swift on macOS and iOS --- USAGE.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/USAGE.md b/USAGE.md index 396d2e84..4395d72d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -63,13 +63,13 @@ module Python { 3. In the “Build Settings” tab, modify the following: - Build Options - - User Script Sandboxing: No - - Enable Testability: Yes + - User Script Sandboxing: ```No``` + - Enable Testability: ```Yes``` - Search Paths - - Framework Search Paths: $(PROJECT_DIR) - - Header Search Paths: "$(BUILT_PRODUCTS_DIR)/Python.framework/Headers" + - Framework Search Paths: ```$(PROJECT_DIR)``` + - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` - Apple Clang - Warnings - All languages - - Quoted Include In Framework Header: No + - Quoted Include In Framework Header: ```No``` 4. In your Swift code, set environment variables when init python (iOS only, macOS do not need this step) From 2eb5fb5975ab47dfa59593560ff973790009d7d4 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:26:37 -0800 Subject: [PATCH 03/20] Update USAGE.md to give more options on Swift --- USAGE.md | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/USAGE.md b/USAGE.md index 4395d72d..d8a880f6 100644 --- a/USAGE.md +++ b/USAGE.md @@ -27,9 +27,9 @@ the iOS guide. ## Accessing the Python runtime -There are 2 ways to access the Python runtime in your project code. +There are 3 ways to access the Python runtime in your project code. -### Embedded C API. +### Use C/Objective-C/C++ with Embedded C API You can use the [Python Embedded C API](https://docs.python.org/3/extending/embedding.html) to instantiate a Python @@ -37,18 +37,10 @@ interpreter. This is the approach taken by Briefcase; you may find the bootstrap mainline code generated by Briefcase a helpful guide to what is needed to start an interpreter and run Python code. -### PythonKit +### Use Swift with Embbed C API +If you want to use Python framework in Swift, should do some additional setups. -An alternate approach is to use -[PythonKit](https://github.com/pvieito/PythonKit). PythonKit is a package that -provides a Swift API to running Python code. - -To use PythonKit in your project: - -1. Add PythonKit to your project using the Swift Package manager. See the - PythonKit documentation for details. - -2. Create a file called `module.modulemap` inside +1. Create a file called `module.modulemap` inside - `Python.xcframework/macos-arm64_x86_64/Python.framework/Versions/3.13/include/python3.13/` (macOS Device), - `Python.xcframework/ios-arm64_x86_64-simulator/Python.framework/Headers/` (iOS Simulator), - `Python.xcframework/ios-arm64/Python.framework/Headers/` (iOS Device), @@ -61,17 +53,12 @@ module Python { } ``` -3. In the “Build Settings” tab, modify the following: -- Build Options - - User Script Sandboxing: ```No``` - - Enable Testability: ```Yes``` +2. In the `Build Settings` tab of your app target, make sure the following paths: - Search Paths - Framework Search Paths: ```$(PROJECT_DIR)``` - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` -- Apple Clang - Warnings - All languages - - Quoted Include In Framework Header: ```No``` -4. In your Swift code, set environment variables when init python (iOS only, macOS do not need this step) +3. In your Swift code, set environment variables when init python (iOS only, macOS do not need this step) ```swift import Python @@ -91,8 +78,7 @@ let appPath = Bundle.main.path(forResource: "app", ofType: nil) setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) ``` - -5. In your Swift code, initialize the Python runtime. This should generally be +4. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: ``` @@ -101,7 +87,23 @@ Py_Initialize() // we now have a Python interpreter ready to be used ``` -6. Invoke Python code in your app. For example: +To integrate 3rd party python code and dependencies, you will need to make sure +`PYTHONPATH` contains their paths; once this has been done, you can run +`Python.import("")`. to import that module from inside swift. + +### Use Swift with `PythonKit` + +An alternate approach in Swift is to use +[PythonKit](https://github.com/pvieito/PythonKit). PythonKit is a package that +provides a Swift API to running Python code. + +To use PythonKit in your project: + +1. Do all steps of "Use Swift with Embbed C API" above +2. Add PythonKit to your project using the Swift Package manager. See the + PythonKit documentation for details. + +3. Invoke Python code in your app. For example: ```swift import PythonKit @@ -112,7 +114,3 @@ print("Python Path: \(sys.path)") _ = Python.import("math") // verifies `lib-dynload` is found and signed successfully ``` - -To integrate 3rd party python code and dependencies, you will need to make sure -`PYTHONPATH` contains their paths; once this has been done, you can run -`Python.import("")`. to import that module from inside swift. From 2444acc0db0ee9072a0bcc356ddfdc9a7aa350ff Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:29:53 -0800 Subject: [PATCH 04/20] Update USAGE.md for more logical title --- USAGE.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/USAGE.md b/USAGE.md index d8a880f6..767b7502 100644 --- a/USAGE.md +++ b/USAGE.md @@ -29,7 +29,7 @@ the iOS guide. There are 3 ways to access the Python runtime in your project code. -### Use C/Objective-C/C++ with Embedded C API +### Use C/Objective-C/C++ You can use the [Python Embedded C API](https://docs.python.org/3/extending/embedding.html) to instantiate a Python @@ -37,7 +37,7 @@ interpreter. This is the approach taken by Briefcase; you may find the bootstrap mainline code generated by Briefcase a helpful guide to what is needed to start an interpreter and run Python code. -### Use Swift with Embbed C API +### Use Swift If you want to use Python framework in Swift, should do some additional setups. 1. Create a file called `module.modulemap` inside @@ -53,12 +53,12 @@ module Python { } ``` -2. In the `Build Settings` tab of your app target, make sure the following paths: +2. In the `Build Settings` tab of your app target, make sure the following paths are set: - Search Paths - Framework Search Paths: ```$(PROJECT_DIR)``` - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` -3. In your Swift code, set environment variables when init python (iOS only, macOS do not need this step) +3. In your Swift code, set environment variables when init Python (iOS only, macOS do not need this step) ```swift import Python @@ -87,10 +87,13 @@ Py_Initialize() // we now have a Python interpreter ready to be used ``` +After init Python, you could use Embed C API in Swift directly + To integrate 3rd party python code and dependencies, you will need to make sure `PYTHONPATH` contains their paths; once this has been done, you can run `Python.import("")`. to import that module from inside swift. + ### Use Swift with `PythonKit` An alternate approach in Swift is to use @@ -99,7 +102,7 @@ provides a Swift API to running Python code. To use PythonKit in your project: -1. Do all steps of "Use Swift with Embbed C API" above +1. Do all steps of "Use Swift" above 2. Add PythonKit to your project using the Swift Package manager. See the PythonKit documentation for details. From ac6e0ec8a6f6c3eec7fccbec07be781749dd15ea Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:33:45 -0800 Subject: [PATCH 05/20] Update USAGE.md for the link of iOS Python official document --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 767b7502..b5c510e4 100644 --- a/USAGE.md +++ b/USAGE.md @@ -20,7 +20,7 @@ what Briefcase is doing). The steps required are documented in the CPython usage guides: * [macOS](https://docs.python.org/3/using/mac.html) -* [iOS](https://docs.python.org/3.14/using/ios.html) +* [iOS](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project) For tvOS and watchOS, you should be able to broadly follow the instructions in the iOS guide. From e676e435df8cc510ef6886098aa29bb1cb2e0d30 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:34:35 -0800 Subject: [PATCH 06/20] Update USAGE.md --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index b5c510e4..447e0ae7 100644 --- a/USAGE.md +++ b/USAGE.md @@ -87,7 +87,7 @@ Py_Initialize() // we now have a Python interpreter ready to be used ``` -After init Python, you could use Embed C API in Swift directly +After init Python, you could use Embed C API in Swift directly. To integrate 3rd party python code and dependencies, you will need to make sure `PYTHONPATH` contains their paths; once this has been done, you can run From 57a02bf13fc79a6d54a240c2fbaa68312a961db1 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:35:14 -0800 Subject: [PATCH 07/20] Update USAGE.md --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 447e0ae7..7cb884ef 100644 --- a/USAGE.md +++ b/USAGE.md @@ -58,7 +58,7 @@ module Python { - Framework Search Paths: ```$(PROJECT_DIR)``` - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` -3. In your Swift code, set environment variables when init Python (iOS only, macOS do not need this step) +3. In your Swift code, set environment variables before init Python (iOS only, macOS do not need this step) ```swift import Python From 599152265ed50b2e4b3eeb90e526071874d40d89 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:35:33 -0800 Subject: [PATCH 08/20] Update USAGE.md --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 7cb884ef..a03eb971 100644 --- a/USAGE.md +++ b/USAGE.md @@ -81,7 +81,7 @@ setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined( 4. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: -``` +```Swift import Python Py_Initialize() // we now have a Python interpreter ready to be used From 82f31ab43edbdee879eedb8a0eca959193324489 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 00:44:51 -0800 Subject: [PATCH 09/20] Update USAGE.md --- USAGE.md | 1 - 1 file changed, 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index a03eb971..8df10c40 100644 --- a/USAGE.md +++ b/USAGE.md @@ -55,7 +55,6 @@ module Python { 2. In the `Build Settings` tab of your app target, make sure the following paths are set: - Search Paths - - Framework Search Paths: ```$(PROJECT_DIR)``` - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` 3. In your Swift code, set environment variables before init Python (iOS only, macOS do not need this step) From b3c190485124e54046af60b347647e912ed530d8 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:33:09 -0800 Subject: [PATCH 10/20] Update USAGE.md --- USAGE.md | 61 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/USAGE.md b/USAGE.md index 8df10c40..58764bf7 100644 --- a/USAGE.md +++ b/USAGE.md @@ -38,26 +38,32 @@ mainline code generated by Briefcase a helpful guide to what is needed to start an interpreter and run Python code. ### Use Swift -If you want to use Python framework in Swift, should do some additional setups. - -1. Create a file called `module.modulemap` inside - - `Python.xcframework/macos-arm64_x86_64/Python.framework/Versions/3.13/include/python3.13/` (macOS Device), - - `Python.xcframework/ios-arm64_x86_64-simulator/Python.framework/Headers/` (iOS Simulator), - - `Python.xcframework/ios-arm64/Python.framework/Headers/` (iOS Device), - containing the following code: -``` -module Python { - umbrella header "Python.h" - export * - link "Python" -} -``` - -2. In the `Build Settings` tab of your app target, make sure the following paths are set: -- Search Paths - - Header Search Paths: ```$(BUILT_PRODUCTS_DIR)/Python.framework/Headers``` - -3. In your Swift code, set environment variables before init Python (iOS only, macOS do not need this step) +If you want to use Python framework in Swift, you should do some additional setups. + +1. Edit all `Python.xcframework/*/Python.framework` + 1. Make a `Modules/module.modulemap` file + ```shell + cd Python.xcframework/macos-arm64_x86_64/Python.framework + mkdir Modules + touch Modules/module.modulemap + ``` + `macos-arm64_x86_64` in the cd command, should be changed to your target platform + + 2. Fill `Modules/module.modulemap` with the content: + ``` + framework module Python { + umbrella header "Python.h" + export * + link "Python" + } + ``` + + 3. Edit `Headers/cpython/pyatomic.h`, in the file: + - replace ``cpython/pyatomic_gcc.h`` with ``pyatomic_gcc.h`` + - replace ``cpython/pyatomic_std.h`` with ``pyatomic_std.h`` + - replace ``cpython/pyatomic_msc.h`` with ``pyatomic_msc.h`` + +2. In your Swift code, set environment variables before init Python (iOS only, macOS do not need this step) ```swift import Python @@ -81,8 +87,12 @@ setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined( done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: ```Swift +import Foundation import Python + Py_Initialize() +let version = String(cString: Py_GetVersion()) +print(version) // we now have a Python interpreter ready to be used ``` @@ -95,9 +105,11 @@ To integrate 3rd party python code and dependencies, you will need to make sure ### Use Swift with `PythonKit` -An alternate approach in Swift is to use -[PythonKit](https://github.com/pvieito/PythonKit). PythonKit is a package that -provides a Swift API to running Python code. +Instead of using Embed C API, an alternate approach is to use +[PythonKit](https://github.com/pvieito/PythonKit). + +PythonKit is a package that +provides Swift friendly API to running Python code. To use PythonKit in your project: @@ -107,8 +119,11 @@ To use PythonKit in your project: 3. Invoke Python code in your app. For example: ```swift +import Foundation +import Python import PythonKit +Py_Initialize() let sys = Python.import("sys") print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)") print("Python Encoding: \(sys.getdefaultencoding().upper())") From b129c51e18d3f3032b1c03f30c5d60776524a2ed Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:38:05 -0800 Subject: [PATCH 11/20] Update USAGE.md --- USAGE.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/USAGE.md b/USAGE.md index 58764bf7..688dc870 100644 --- a/USAGE.md +++ b/USAGE.md @@ -29,7 +29,7 @@ the iOS guide. There are 3 ways to access the Python runtime in your project code. -### Use C/Objective-C/C++ +### Use C/Objective-C/C++ with Embedded C API You can use the [Python Embedded C API](https://docs.python.org/3/extending/embedding.html) to instantiate a Python @@ -37,7 +37,7 @@ interpreter. This is the approach taken by Briefcase; you may find the bootstrap mainline code generated by Briefcase a helpful guide to what is needed to start an interpreter and run Python code. -### Use Swift +### Use Swift with Embedded C API If you want to use Python framework in Swift, you should do some additional setups. 1. Edit all `Python.xcframework/*/Python.framework` @@ -113,7 +113,7 @@ provides Swift friendly API to running Python code. To use PythonKit in your project: -1. Do all steps of "Use Swift" above +1. Do all steps of "Use Swift with Embedded C API" above 2. Add PythonKit to your project using the Swift Package manager. See the PythonKit documentation for details. From b9bfae5004a267b31f85834f471ac5135b27ba06 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:40:24 -0800 Subject: [PATCH 12/20] Update USAGE.md --- USAGE.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/USAGE.md b/USAGE.md index 688dc870..0873a40b 100644 --- a/USAGE.md +++ b/USAGE.md @@ -67,20 +67,23 @@ If you want to use Python framework in Swift, you should do some additional setu ```swift import Python - -guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } -setenv("PYTHONHOME", pythonHome, 1) - -/* - The PYTHONPATH for the interpreter includes: - the python/lib/python3.X subfolder of your app’s bundle, - the python/lib/python3.X/lib-dynload subfolder of your app’s bundle, and - the app subfolder of your app’s bundle -*/ -guard let pythonPath = Bundle.main.path(forResource: "python/lib/python3.13", ofType: nil) else { return } -guard let libDynLoad = Bundle.main.path(forResource: "python/lib/python3.13/lib-dynload", ofType: nil) else { return } -let appPath = Bundle.main.path(forResource: "app", ofType: nil) -setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) +func setEnvs() { + #if not os(macOS) + guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } + setenv("PYTHONHOME", pythonHome, 1) + + /* + The PYTHONPATH for the interpreter includes: + the python/lib/python3.X subfolder of your app’s bundle, + the python/lib/python3.X/lib-dynload subfolder of your app’s bundle, and + the app subfolder of your app’s bundle + */ + guard let pythonPath = Bundle.main.path(forResource: "python/lib/python3.13", ofType: nil) else { return } + guard let libDynLoad = Bundle.main.path(forResource: "python/lib/python3.13/lib-dynload", ofType: nil) else { return } + let appPath = Bundle.main.path(forResource: "app", ofType: nil) + setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) + #endif +} ``` 4. In your Swift code, initialize the Python runtime. This should generally be @@ -90,6 +93,7 @@ setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined( import Foundation import Python +setEnvs() Py_Initialize() let version = String(cString: Py_GetVersion()) print(version) @@ -123,6 +127,7 @@ import Foundation import Python import PythonKit +setEnvs() Py_Initialize() let sys = Python.import("sys") print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)") From a9f4fcd674d10d08260cc0c02b9d88d049bfb41f Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:41:18 -0800 Subject: [PATCH 13/20] Update USAGE.md --- USAGE.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 0873a40b..d748048f 100644 --- a/USAGE.md +++ b/USAGE.md @@ -68,7 +68,9 @@ If you want to use Python framework in Swift, you should do some additional setu ```swift import Python func setEnvs() { - #if not os(macOS) + #if os(macOS) + print("macOS do not need set envs") + #else guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } setenv("PYTHONHOME", pythonHome, 1) From 4747f22193b2219dd1e1c8dad50e4547cf49d442 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:42:10 -0800 Subject: [PATCH 14/20] Update USAGE.md --- USAGE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/USAGE.md b/USAGE.md index d748048f..6de0d6e6 100644 --- a/USAGE.md +++ b/USAGE.md @@ -132,6 +132,7 @@ import PythonKit setEnvs() Py_Initialize() let sys = Python.import("sys") +print("Python Full Version: \(sys.version)") print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)") print("Python Encoding: \(sys.getdefaultencoding().upper())") print("Python Path: \(sys.path)") From 10e844ef4b10bb65a9adfb143e77b442e16ccd23 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:50:47 -0800 Subject: [PATCH 15/20] Update USAGE.md --- USAGE.md | 97 +++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/USAGE.md b/USAGE.md index 6de0d6e6..387ad4b4 100644 --- a/USAGE.md +++ b/USAGE.md @@ -63,44 +63,40 @@ If you want to use Python framework in Swift, you should do some additional setu - replace ``cpython/pyatomic_std.h`` with ``pyatomic_std.h`` - replace ``cpython/pyatomic_msc.h`` with ``pyatomic_msc.h`` -2. In your Swift code, set environment variables before init Python (iOS only, macOS do not need this step) - -```swift -import Python -func setEnvs() { - #if os(macOS) - print("macOS do not need set envs") - #else - guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } - setenv("PYTHONHOME", pythonHome, 1) - - /* - The PYTHONPATH for the interpreter includes: - the python/lib/python3.X subfolder of your app’s bundle, - the python/lib/python3.X/lib-dynload subfolder of your app’s bundle, and - the app subfolder of your app’s bundle - */ - guard let pythonPath = Bundle.main.path(forResource: "python/lib/python3.13", ofType: nil) else { return } - guard let libDynLoad = Bundle.main.path(forResource: "python/lib/python3.13/lib-dynload", ofType: nil) else { return } - let appPath = Bundle.main.path(forResource: "app", ofType: nil) - setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) - #endif -} -``` - -4. In your Swift code, initialize the Python runtime. This should generally be +2. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: -```Swift -import Foundation -import Python -setEnvs() -Py_Initialize() -let version = String(cString: Py_GetVersion()) -print(version) -// we now have a Python interpreter ready to be used -``` + ```swift + import Foundation + import Python + + func setEnvs() { + #if os(macOS) + print("macOS do not need set envs") + #else + guard let pythonHome = Bundle.main.path(forResource: "python", ofType: nil) else { return } + setenv("PYTHONHOME", pythonHome, 1) + + /* + The PYTHONPATH for the interpreter includes: + the python/lib/python3.X subfolder of your app’s bundle, + the python/lib/python3.X/lib-dynload subfolder of your app’s bundle, and + the app subfolder of your app’s bundle + */ + guard let pythonPath = Bundle.main.path(forResource: "python/lib/python3.13", ofType: nil) else { return } + guard let libDynLoad = Bundle.main.path(forResource: "python/lib/python3.13/lib-dynload", ofType: nil) else { return } + let appPath = Bundle.main.path(forResource: "app", ofType: nil) + setenv("PYTHONPATH", [pythonPath, libDynLoad, appPath].compactMap { $0 }.joined(separator: ":"), 1) + #endif + } + + setEnvs() + Py_Initialize() + let version = String(cString: Py_GetVersion()) + print(version) + // we now have a Python interpreter ready to be used + ``` After init Python, you could use Embed C API in Swift directly. @@ -124,18 +120,19 @@ To use PythonKit in your project: PythonKit documentation for details. 3. Invoke Python code in your app. For example: -```swift -import Foundation -import Python -import PythonKit - -setEnvs() -Py_Initialize() -let sys = Python.import("sys") -print("Python Full Version: \(sys.version)") -print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)") -print("Python Encoding: \(sys.getdefaultencoding().upper())") -print("Python Path: \(sys.path)") - -_ = Python.import("math") // verifies `lib-dynload` is found and signed successfully -``` + ```swift + import Foundation + import Python + + // ... setEnv defines + setEnvs() + Py_Initialize() + + import PythonKit + let sys = Python.import("sys") + print("Python Full Version: \(sys.version)") + print("Python Version: \(sys.version_info.major).\(sys.version_info.minor)") + print("Python Encoding: \(sys.getdefaultencoding().upper())") + print("Python Path: \(sys.path)") + _ = Python.import("math") // verifies `lib-dynload` is found and signed successfully + ``` From 4eb7036e7589352933c7f6e92f0c3d78359f4042 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:52:48 -0800 Subject: [PATCH 16/20] Update USAGE.md --- USAGE.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/USAGE.md b/USAGE.md index 387ad4b4..cb8e5fd3 100644 --- a/USAGE.md +++ b/USAGE.md @@ -62,8 +62,9 @@ If you want to use Python framework in Swift, you should do some additional setu - replace ``cpython/pyatomic_gcc.h`` with ``pyatomic_gcc.h`` - replace ``cpython/pyatomic_std.h`` with ``pyatomic_std.h`` - replace ``cpython/pyatomic_msc.h`` with ``pyatomic_msc.h`` - -2. In your Swift code, initialize the Python runtime. This should generally be +2. Drag `Python.xcframework` into the root of the macOS/iOS project in Xcode Navigator + +4. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: From 067a5619968311d8a6c4c9984941938fa70a3c52 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:56:26 -0800 Subject: [PATCH 17/20] Update USAGE.md --- USAGE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index cb8e5fd3..5500c488 100644 --- a/USAGE.md +++ b/USAGE.md @@ -62,8 +62,9 @@ If you want to use Python framework in Swift, you should do some additional setu - replace ``cpython/pyatomic_gcc.h`` with ``pyatomic_gcc.h`` - replace ``cpython/pyatomic_std.h`` with ``pyatomic_std.h`` - replace ``cpython/pyatomic_msc.h`` with ``pyatomic_msc.h`` + 2. Drag `Python.xcframework` into the root of the macOS/iOS project in Xcode Navigator - +3. If you are making an iOS project, do the steps of 3-9 in the Python offical document [Adding Python to an iOS Project](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project). 4. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: From 831c75d87b5f8a3d3667949cda924f7edc2bdbae Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 11:58:01 -0800 Subject: [PATCH 18/20] Update USAGE.md --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 5500c488..9fc22247 100644 --- a/USAGE.md +++ b/USAGE.md @@ -64,7 +64,7 @@ If you want to use Python framework in Swift, you should do some additional setu - replace ``cpython/pyatomic_msc.h`` with ``pyatomic_msc.h`` 2. Drag `Python.xcframework` into the root of the macOS/iOS project in Xcode Navigator -3. If you are making an iOS project, do the steps of 3-9 in the Python offical document [Adding Python to an iOS Project](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project). +3. If you are making an iOS project, do the steps of 3, 6, 7, 8, 9 in the Python offical document [Adding Python to an iOS Project](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project). 4. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: From 4ebd11cd3d648ee9fe3b96190a9ed03b6ec3e0db Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 12:01:38 -0800 Subject: [PATCH 19/20] Update USAGE.md --- USAGE.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 9fc22247..dcd1ddf7 100644 --- a/USAGE.md +++ b/USAGE.md @@ -65,7 +65,9 @@ If you want to use Python framework in Swift, you should do some additional setu 2. Drag `Python.xcframework` into the root of the macOS/iOS project in Xcode Navigator 3. If you are making an iOS project, do the steps of 3, 6, 7, 8, 9 in the Python offical document [Adding Python to an iOS Project](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project). -4. In your Swift code, initialize the Python runtime. This should generally be + `iOS/Resources/dylib-Info-template.plist` for the step 3 is [here]([bee7bb3310b356e99e3a0f75f23efbc97f1b0a24/iOS/Resources/dylib-Info-template.plist](https://github.com/python/cpython/blob/bee7bb3310b356e99e3a0f75f23efbc97f1b0a24/iOS/Resources/dylib-Info-template.plist)) + +5. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely needs to be done before you invoke Python code: From 2b6459ffc8bcd6cf8b9f29ee0841c1b4d06b1c01 Mon Sep 17 00:00:00 2001 From: frogcjn Date: Fri, 14 Feb 2025 12:02:21 -0800 Subject: [PATCH 20/20] Update USAGE.md --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index dcd1ddf7..1e681709 100644 --- a/USAGE.md +++ b/USAGE.md @@ -65,7 +65,7 @@ If you want to use Python framework in Swift, you should do some additional setu 2. Drag `Python.xcframework` into the root of the macOS/iOS project in Xcode Navigator 3. If you are making an iOS project, do the steps of 3, 6, 7, 8, 9 in the Python offical document [Adding Python to an iOS Project](https://docs.python.org/3/using/ios.html#adding-python-to-an-ios-project). - `iOS/Resources/dylib-Info-template.plist` for the step 3 is [here]([bee7bb3310b356e99e3a0f75f23efbc97f1b0a24/iOS/Resources/dylib-Info-template.plist](https://github.com/python/cpython/blob/bee7bb3310b356e99e3a0f75f23efbc97f1b0a24/iOS/Resources/dylib-Info-template.plist)) + `iOS/Resources/dylib-Info-template.plist` for the step 3 is [here](https://github.com/python/cpython/blob/bee7bb3310b356e99e3a0f75f23efbc97f1b0a24/iOS/Resources/dylib-Info-template.plist)) 5. In your Swift code, initialize the Python runtime. This should generally be done as early as possible in the application's lifecycle, but definitely