forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializePython.swift
More file actions
37 lines (33 loc) · 1.09 KB
/
InitializePython.swift
File metadata and controls
37 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Foundation
import Logger
import Python
import PythonKit
import PythonResources
@PythonActor
var isPythonInitialized = false
/// Initialize Python.
@PythonActor
public func initializePython() {
guard !isPythonInitialized else { return }
guard let sitePackagePath, let stdLibPath, let libDynloadPath else {
assertionFailure("Python is not installed! Please run `make setup` to install Python.")
Logger.python.info("Python is not installed!")
return
}
setenv("PYTHONHOME", stdLibPath, 1)
setenv("PYTHONPATH", "\(stdLibPath):\(libDynloadPath):\(sitePackagePath)", 1)
setenv("PYTHONIOENCODING", "utf-8", 1)
// Initialize python
Py_Initialize()
isPythonInitialized = true
// Immediately release the thread, so that we can ensure the GIL state later.
_ = PyEval_SaveThread()
Task {
// All future task should run inside runPython.
try runPython {
let sys = Python.import("sys")
Logger.service
.info("Python Version: \(sys.version_info.major).\(sys.version_info.minor)")
}
}
}