0

嗨,我正在构建一个简单的应用程序,它将有一个组合框来选择位于名为 scripts 的目录中的 python 脚本,以在数据库上创建报告。我打算使用 py2exe 来构建程序,这样用户就不必安装 python 和子模块。

那么我将如何让程序使用 py2exe dist 运行这些脚本呢?

我考虑过使用 system('command') 并从我的安装目录复制 python.exe 以运行 system(os.curdir+'python.exe ' + script_to_run) 然后 python.exe 将使用 python 的本地副本。 dll 和它需要运行的库,即 reportlab 和 pyobdc

那会起作用还是有更好的方法?

(如果这样更容易的话,我也不介意在 Ironpython 中构建它)

4

1 回答 1

0

Micheal Foord 有以下从 Ironpython 中嵌入 Ironpython 的示例

基本步骤是创建您的 shell 应用程序。对于真正的轻量级 GUI,有EasyWPF。使用 pyc 将脚本编译为 exe 并将标准库编译为 DLL。根据您是否需要从脚本中捕获标准输出或将信息中的变量传递给它们,事情可能会变得更加复杂,如文章中所述。下面列出了一个基本示例。

import clr
clr.AddReference('IronPython')
clr.AddReference('System')
clr.AddReference('mscorlib')
clr.AddReference('Microsoft.Scripting')
clr.AddReference('MyStandardLib')
#Now you can reference things like 'import lxml as L .....

from IronPython.Hosting import Python
from Microsoft.Scripting import SourceCodeKind

spath = "C:/fred.py"  # Path to script


engine = Python.CreateEngine()
#Now add your assemblies (like the standard library) to the engine
for assembly in clr.References:
    runtime.LoadAssembly(assembly)

source = engine.CreateScriptSourceFromFile(spath, SourceCodeKind.Statements)
mod = engine.CreateScope()
runtime = engine.Runtime

source.Execute(mod)
于 2011-02-04T17:58:01.257 回答