SwiftUI - 打包 Python 和模块库
SwiftUI - packing with Python and a module library
我的目标:在我的 Swift 项目中通过 PythonKit Python 3.9.1 导入,在那里我可以使用它将它与所需的模块“openpyxl”(用于用值填充 excel sheet)。
以下是特定 python 文件开头的导入:
# some imports
import sys
from openpyxl import load_workbook # Library for editing xlsx-files
import datetime # for current date-informations
import csv # for reading csv
如您所见,我需要 Python 3,但大多数 Mac 都没有安装它。所以我想将它包含在我的应用程序中。我是 Swift 的新手,不知道如何实现。
到目前为止我做了什么:
- 为“PythonKit”添加了 Swift 包依赖(使用 Github Link 并选择了“master”分支)
- 正在浏览 Google 如何在 swift 项目中包含特定的 Python 版本(并使用它们来执行 Python 文件)
- 询问Google如何将“openpyxl”之类的库添加到PythonKit/Swift
你能带我进一步吗?对于如何在我的 Swift 应用程序中使用这些 python 的东西,我很感激。如果能集成 python 库并使其在每台机器上运行,那就太好了。
找到解决方案:
使用 py2app 创建一个 Python.plugin 包,将其加载到 Xcode 并使用 python 二进制文件的路径。
这是您可以使用的setup.py:
"""
This is a setup.py script to create a py2app plugin bundle
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['RandomFile.py']
OPTIONS = {
# Any local packages to include in the bundle should go here.
# See the py2app documentation for more.
"includes": [],
}
setup(
plugin=APP,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
install_requires=['openpyxl'],
)
您可以在此处找到使用 py2app 构建的文档:https://py2app.readthedocs.io/en/latest/index.html
构建插件后,我们可以将它拖到我们的 Xcode 项目中,并在 Swift 中获取它的路径,如下所示:
let pluginPath = Bundle.main.path(forResource: "Bridge", ofType: "plugin")
我的目标:在我的 Swift 项目中通过 PythonKit Python 3.9.1 导入,在那里我可以使用它将它与所需的模块“openpyxl”(用于用值填充 excel sheet)。
以下是特定 python 文件开头的导入:
# some imports
import sys
from openpyxl import load_workbook # Library for editing xlsx-files
import datetime # for current date-informations
import csv # for reading csv
如您所见,我需要 Python 3,但大多数 Mac 都没有安装它。所以我想将它包含在我的应用程序中。我是 Swift 的新手,不知道如何实现。
到目前为止我做了什么:
- 为“PythonKit”添加了 Swift 包依赖(使用 Github Link 并选择了“master”分支)
- 正在浏览 Google 如何在 swift 项目中包含特定的 Python 版本(并使用它们来执行 Python 文件)
- 询问Google如何将“openpyxl”之类的库添加到PythonKit/Swift
你能带我进一步吗?对于如何在我的 Swift 应用程序中使用这些 python 的东西,我很感激。如果能集成 python 库并使其在每台机器上运行,那就太好了。
找到解决方案:
使用 py2app 创建一个 Python.plugin 包,将其加载到 Xcode 并使用 python 二进制文件的路径。
这是您可以使用的setup.py:
"""
This is a setup.py script to create a py2app plugin bundle
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['RandomFile.py']
OPTIONS = {
# Any local packages to include in the bundle should go here.
# See the py2app documentation for more.
"includes": [],
}
setup(
plugin=APP,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
install_requires=['openpyxl'],
)
您可以在此处找到使用 py2app 构建的文档:https://py2app.readthedocs.io/en/latest/index.html
构建插件后,我们可以将它拖到我们的 Xcode 项目中,并在 Swift 中获取它的路径,如下所示:
let pluginPath = Bundle.main.path(forResource: "Bridge", ofType: "plugin")