构建一个您可以分发的 Ironpython EXE 有点棘手——尤其是在您使用标准库的元素时。我的典型解决方案如下:
我将我需要的所有 stdlib 模块复制到一个文件夹中(通常所有这些模块只是为了完整性)并使用这个脚本来构建我的 exe。在这个例子中,我有两个文件 FredMain.py 和 FredSOAP.py 被编译成一个名为 Fred_Download_Tool
的 EXE
import sys
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib')
sys.path.append(r'C:\Program Files\IronPython 2.7')
import clr
clr.AddReference('IronPython')
clr.AddReference('IronPython.Modules')
clr.AddReference('Microsoft.Scripting.Metadata')
clr.AddReference('Microsoft.Scripting')
clr.AddReference('Microsoft.Dynamic')
clr.AddReference('mscorlib')
clr.AddReference('System')
clr.AddReference('System.Data')
#
# adapted from os-path-walk-example-3.py
import os, glob
import fnmatch
import pyc
def doscopy(filename1):
print filename1
os.system ("copy %s .\\bin\Debug\%s" % (filename1, filename1))
class GlobDirectoryWalker:
# a forward iterator that traverses a directory tree
def __init__(self, directory, pattern="*"):
self.stack = [directory]
self.pattern = pattern
self.files = []
self.index = 0
def __getitem__(self, index):
while 1:
try:
file = self.files[self.index]
self.index = self.index + 1
except IndexError:
# pop next directory from stack
self.directory = self.stack.pop()
self.files = os.listdir(self.directory)
self.index = 0
else:
# got a filename
fullname = os.path.join(self.directory, file)
if os.path.isdir(fullname) and not os.path.islink(fullname) and fullname[-4:]<>'.svn':
self.stack.append(fullname)
if fnmatch.fnmatch(file, self.pattern):
return fullname
#Build StdLib.DLL
gb = glob.glob(r".\Lib\*.py")
gb.append("/out:StdLib")
#print ["/target:dll",]+gb
pyc.Main(["/target:dll"]+gb)
#Build EXE
gb=["/main:FredMain.py","FredSOAP.py","/target:exe","/out:Fred_Download_Tool"]
pyc.Main(gb)
#CopyFiles to Release Directory
doscopy("StdLib.dll")
doscopy("Fred_Download_Tool.exe")
doscopy("Fred_Download_.dll")
#Copy DLLs to Release Directory
fl = ["IronPython.dll","IronPython.Modules.dll","Microsoft.Dynamic.dll","Microsoft.Scripting.Debugging.dll","Microsoft.Scripting.dll","Microsoft.Scripting.ExtensionAttribute.dll","Microsoft.Scripting.Core.dll"]
for f in fl:
doscopy(f)
当我准备好编译时,我会在我的脚本中添加以下内容。这允许程序使用我的 DLL 中的标准模块而不是 Python 安装。如果你想分发给没有安装 Python 的人,这是必要的。只需确保在创建安装程序时包含必要的 DLL。
#References to created DLL of python modules
clr.AddReference('StdLib')