I used Python-3.11-macOS-support.b1.tar.gz and imported both python-stdlib and Python.xcframework. Here is what the version file says Python version: 3.11.0
Build: b1
Min macOS version: 10.15
libFFI: macOS native
BZip2: 1.0.8
OpenSSL: 3.0.5
XZ: 5.2.6
Here is my swift code: import SwiftUI
import Python
import PythonKit
import Foundation
struct PythonInfo {
var version: String = ""
var encoding: String = ""
var path: String = ""
}
class EmbeddedPython: ObservableObject {
@ Published var pythonInfo = PythonInfo()
private let logFile: URL
init() {
let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let appFolder = appSupport.appendingPathComponent("PythonSwiftoo", isDirectory: true)
try? FileManager.default.createDirectory(at: appFolder, withIntermediateDirectories: true)
logFile = appFolder.appendingPathComponent("python_log.txt")
logMessage("EmbeddedPython initialization started")
logMessage("Current working directory: \(FileManager.default.currentDirectoryPath)")
// Log environment variables
for (key, value) in ProcessInfo.processInfo.environment {
logMessage("Environment variable: \(key) = \(value)")
}
if let stdLibPath = Bundle.main.path(forResource: "python-stdlib", ofType: nil),
let libDynloadPath = Bundle.main.path(forResource: "python-stdlib/lib-dynload", ofType: nil)
{
logMessage("Found stdlib path: \(stdLibPath)")
logMessage("Found lib-dynload path: \(libDynloadPath)")
// Log contents of lib-dynload directory
if let contents = try? FileManager.default.contentsOfDirectory(atPath: libDynloadPath) {
logMessage("Contents of lib-dynload: \(contents)")
}
setenv("PYTHONHOME", stdLibPath, 1)
setenv("PYTHONPATH", "\(stdLibPath):\(libDynloadPath)", 1)
logMessage("Environment variables set")
logMessage("PYTHONHOME set to: \(String(cString: getenv("PYTHONHOME")))")
logMessage("PYTHONPATH set to: \(String(cString: getenv("PYTHONPATH")))")
logMessage("Before Py_Initialize()")
Py_Initialize()
logMessage("After Py_Initialize()")
} else {
logMessage("Failed to find Python paths")
}
updatePythonInfo()
}
func updatePythonInfo() {
logMessage("Updating Python info")
do {
let sys = try Python.import("sys")
logMessage("sys module imported")
self.pythonInfo.version = "\(sys.version_info.major).\(sys.version_info.minor)"
self.pythonInfo.encoding = "\(sys.getdefaultencoding().upper())"
self.pythonInfo.path = "\(sys.path)"
logMessage("Python version: \(self.pythonInfo.version)")
logMessage("Python encoding: \(self.pythonInfo.encoding)")
logMessage("Python path: \(self.pythonInfo.path)")
//logMessage("sys.prefix: \(sys.prefix)")
logMessage("sys.exec_prefix: \(sys.exec_prefix)")
logMessage("sys.executable: \(sys.executable)")
let os = try Python.import("os")
logMessage("Current working directory (from Python): \(os.getcwd())")
_ = try Python.import("math")
logMessage("math module imported successfully")
} catch {
logMessage("Error in updatePythonInfo: \(error)")
}
}
func logMessage(_ message: String) {
let timestamp = ISO8601DateFormatter().string(from: Date())
let logEntry = "[\(timestamp)] \(message)\n"
do {
if FileManager.default.fileExists(atPath: logFile.path) {
let fileHandle = try FileHandle(forWritingTo: logFile)
fileHandle.seekToEndOfFile()
fileHandle.write(logEntry.data(using: .utf8)!)
fileHandle.closeFile()
} else {
try logEntry.write(to: logFile, atomically: true, encoding: .utf8)
}
} catch {
print("Failed to write to log file: \(error.localizedDescription)")
}
}
}
@ main
struct PythonSwiftooApp: App {
@ StateObject var embeddedPython = EmbeddedPython()
var body: some Scene {
WindowGroup {
ContentView(embeddedPython: embeddedPython)
.onAppear {
embeddedPython.logMessage("App appeared")
}
}
}
} and yet if you look at the logs it says python version 3.12 and the app crashes after it launches [2024-09-05T22:38:16Z] EmbeddedPython initialization started
[2024-09-05T22:38:16Z] Current working directory: /
[2024-09-05T22:38:16Z] Environment variable: XPC_FLAGS = 0x0
[2024-09-05T22:38:16Z] Environment variable: TMPDIR = /var/folders/bc/hwycx8wx3151dwgk0nkwv2c00000gn/T/
[2024-09-05T22:38:16Z] Environment variable: HOME = /Users/ajr
[2024-09-05T22:38:16Z] Environment variable: PATH = /usr/bin:/bin:/usr/sbin:/sbin
[2024-09-05T22:38:16Z] Environment variable: USER = ajr
[2024-09-05T22:38:16Z] Environment variable: SHELL = /bin/zsh
[2024-09-05T22:38:16Z] Environment variable: COMMAND_MODE = unix2003
[2024-09-05T22:38:16Z] Environment variable: LOGNAME = ajr
[2024-09-05T22:38:16Z] Environment variable: XPC_SERVICE_NAME = application.com.rolodexguru.PythonSwiftoo.2723382.2723388
[2024-09-05T22:38:16Z] Environment variable: __CFBundleIdentifier = com.rolodexguru.PythonSwiftoo
[2024-09-05T22:38:16Z] Environment variable: SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.CMfn38d7Yi/Listeners
[2024-09-05T22:38:16Z] Environment variable: __CF_USER_TEXT_ENCODING = 0x1F5:0x0:0x0
[2024-09-05T22:38:16Z] Found stdlib path: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib
[2024-09-05T22:38:16Z] Found lib-dynload path: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib-dynload
[2024-09-05T22:38:16Z] Contents of lib-dynload: ["_sqlite3.cpython-311-darwin.so", "_scproxy.cpython-311-darwin.so", "_md5.cpython-311-darwin.so", "_multiprocessing.cpython-311-darwin.so", "_heapq.cpython-311-darwin.so", "_codecs_kr.cpython-311-darwin.so", "_opcode.cpython-311-darwin.so", "_codecs_tw.cpython-311-darwin.so", "_uuid.cpython-311-darwin.so", "_elementtree.cpython-311-darwin.so", "cmath.cpython-311-darwin.so", "_crypt.cpython-311-darwin.so", "_lsprof.cpython-311-darwin.so", "resource.cpython-311-darwin.so", "_codecs_hk.cpython-311-darwin.so", "audioop.cpython-311-darwin.so", "_contextvars.cpython-311-darwin.so", "_random.cpython-311-darwin.so", "_json.cpython-311-darwin.so", "binascii.cpython-311-darwin.so", "_datetime.cpython-311-darwin.so", "mmap.cpython-311-darwin.so", "nis.cpython-311-darwin.so", "_bz2.cpython-311-darwin.so", "_codecs_jp.cpython-311-darwin.so", "_ctypes.cpython-311-darwin.so", "math.cpython-311-darwin.so", "_codecs_iso2022.cpython-311-darwin.so", "_curses.cpython-311-darwin.so", "_dbm.cpython-311-darwin.so", "_ctypes_test.cpython-311-darwin.so", "unicodedata.cpython-311-darwin.so", "termios.cpython-311-darwin.so", "zlib.cpython-311-darwin.so", "_sha256.cpython-311-darwin.so", "_bisect.cpython-311-darwin.so", "_asyncio.cpython-311-darwin.so", "_codecs_cn.cpython-311-darwin.so", "_multibytecodec.cpython-311-darwin.so", "_sha512.cpython-311-darwin.so", "_posixsubprocess.cpython-311-darwin.so", "_pickle.cpython-311-darwin.so", "pyexpat.cpython-311-darwin.so", "select.cpython-311-darwin.so", "_lzma.cpython-311-darwin.so", "readline.cpython-311-darwin.so", "_typing.cpython-311-darwin.so", "syslog.cpython-311-darwin.so", "_blake2.cpython-311-darwin.so", "_queue.cpython-311-darwin.so", "array.cpython-311-darwin.so", "_struct.cpython-311-darwin.so", "_zoneinfo.cpython-311-darwin.so", "_posixshmem.cpython-311-darwin.so", "_sha1.cpython-311-darwin.so", "_decimal.cpython-311-darwin.so", "_socket.cpython-311-darwin.so", "_hashlib.cpython-311-darwin.so", "_curses_panel.cpython-311-darwin.so", "grp.cpython-311-darwin.so", "_ssl.cpython-311-darwin.so", "_sha3.cpython-311-darwin.so", "fcntl.cpython-311-darwin.so", "_statistics.cpython-311-darwin.so", "_csv.cpython-311-darwin.so"]
[2024-09-05T22:38:16Z] Environment variables set
[2024-09-05T22:38:16Z] PYTHONHOME set to: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib
[2024-09-05T22:38:16Z] PYTHONPATH set to: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib:/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib-dynload
[2024-09-05T22:38:16Z] Before Py_Initialize()
[2024-09-05T22:38:16Z] After Py_Initialize()
[2024-09-05T22:38:16Z] Updating Python info
[2024-09-05T22:38:16Z] sys module imported
[2024-09-05T22:38:16Z] Python version: 3.12
[2024-09-05T22:38:16Z] Python encoding: UTF-8
[2024-09-05T22:38:16Z] Python path: ['/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib', '/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib-dynload', '/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib/python312.zip', '/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib/python3.12', '/Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/lib/python3.12/lib-dynload']
[2024-09-05T22:38:16Z] sys.exec_prefix: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib
[2024-09-05T22:38:16Z] sys.executable: /Users/ajr/Desktop/PythonSwiftoo_More_logs.app/Contents/Resources/python-stdlib/bin/python3.12
[2024-09-05T22:38:16Z] Current working directory (from Python): / . Am I missing something basic?