我使用 PythonKit 和 XCode 从 swift 代码调用 spaCy API。从 Home-brew 安装了 PythonKit,将其导入到我的项目中并且构建得很好;但是,在运行时我收到错误:
Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
Run Code Online (Sandbox Code Playgroud)
所以我编译了PythonTools(在PythonKit目录中,运行swift package generate-xcodeproj并打开PythonKit.xcodeproj文件)来查看它会找到Python库的哪些路径:
/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip
/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7
/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload
/Users/fernandabrum/Library/Python/3.7/lib/python/site-packages
/usr/local/lib/python3.7/site-packages
Run Code Online (Sandbox Code Playgroud)
如果我的系统中存在 Python 库,为什么会出现“未找到 Python 库”的错误?我究竟做错了什么?
OBS.:我还尝试设置 PYTHON_LIBRARYexport PYTHON_LIBRARY=和上面找到的路径,但不成功。
提前致谢!
我正在尝试在我的 Swift 项目中使用 PythonKit 来使用一些 Python 代码。为此,我下载了新的 Xcode 11 以将 PythonKit 添加为 Swift 包。添加带有 Swift Package 的 PythonKit 后,我在这里就有了这些项目依赖项。

我想在应用程序启动时使用 Python 代码,因此我将对 Python 代码的调用放在我的应用程序委托中的应用程序函数中。
import UIKit
import PythonKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let sys = Python.import("sys")
print("Python Version: \(sys.version)")
sys.path.append("/Users/me/Documents/MyProject/MyPackage/my_python_main_file")
let pythonAPI = Python.import("my_python_main_file")
pythonAPI.main_function.call()
return true
}
Run Code Online (Sandbox Code Playgroud)
运行我的项目后,出现以下错误:
致命错误:找不到 Python 库。使用 Python 库的路径设置 PYTHON_LIBRARY 环境变量。 …