use*_*215 18 ironpython compilation
我已经尝试使用py2exe(与ipy不兼容)和PYC(过时).任何人都可以指向一个好的编译器的方向?
Chr*_*ian 29
您可以使用pyc.py,在Python的命令行编译器,它包括在IronPython的,因为2.6版编译一个Python脚本的可执行文件.你可以在%IRONPYTONINSTALLDIR%\Tools\Scripts\pyc.py你的硬盘上找到它.
让我们假设您有一个简单的脚本test.py,只是打印出一些东西到控制台.您可以使用以下命令行将其转换为可执行文件(假设IronPython目录是当前目录,test.py并且也在那里):
ipy.exe Tools\Scripts\pyc.py /main:test.py /target:exe
Run Code Online (Sandbox Code Playgroud)
注意:如果您正在使用表单而不希望打开控制台窗口,则需要使用/target:winexe而不是/target:exe.
结果将是两个文件,test.dll和test.exe.test.dll将包含您的实际脚本代码,而test.exe只是一个启动器test.dll.如果包含文件,则可以将此EXE和DLL分发给未安装IronPython的其他计算机
IronPython.dll,Microsoft.Dynamic.dll,Microsoft.Scripting.Core.dll,Microsoft.Scripting.Debugging.dll,Microsoft.Scripting.dll,Microsoft.Scripting.ExtensionAttribute.dll 和IronPython.Modules.dll (有时需要).另请参阅博客条目IronPython - 如何编译exe.
这是一个长期存在的问题,关于互联网上的信息很少.我能找到的唯一已知解决方案是使用SharpDevelop的http://community.sharpdevelop.net/blogs/mattward/archive/2010/03/16/CompilingPythonPackagesWithIronPython.aspx.但是,这个解决方案是不切实际的,因为任何半复杂的python项目都会进行大量的模块导入,SharpDevelop解决方案要求你为每个导入创建一个项目.我开始使用它并在大约30个新项目之后放弃了,更好地编写了一个自动化解决方案!
所以这是我的解决方案,我现在就警告你,它没有作为一个合理的项目被释放出于正当理由:
#!/usr/bin/env python
# CompileToStandalone, a Python to .NET ILR compiler which produces standalone binaries
# (C) 2012 Niall Douglas http://www.nedproductions.biz/
# Created: March 2012
import modulefinder, sys, os, subprocess, _winreg
if len(sys.argv)<2:
print("Usage: CompileEverythingToILR.py <source py> [-outdir=<dest dir>]")
sys.exit(0)
if sys.platform=="cli":
print("ERROR: IronPython's ModuleFinder currently doesn't work, so run me under CPython please")
sys.exit(1)
sourcepath=sys.argv[1]
destpath=sys.argv[2][8:] if len(sys.argv)==3 else os.path.dirname(sys.argv[0])
ironpythonpath=None
try:
try:
keyh=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\IronPython\\2.7\\InstallPath")
ironpythonpath=_winreg.QueryValue(keyh, None)
except Exception as e:
try:
keyh=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\IronPython\\2.7\\InstallPath")
ironpythonpath=_winreg.QueryValue(keyh, "")
except Exception as e:
pass
finally:
if ironpythonpath is not None:
_winreg.CloseKey(keyh)
print("IronPython found at "+ironpythonpath)
else:
raise Exception("Cannot find IronPython in the registry")
# What we do now is to load the python source but against the customised IronPython runtime
# library which has been hacked to work with IronPython. This spits out the right set of
# modules mostly, but we include the main python's site-packages in order to resolve any
# third party packages
print("Scanning '"+sourcepath+"' for dependencies and outputting into '"+destpath+"' ...")
searchpaths=[".", ironpythonpath+os.sep+"Lib"]
searchpaths+=[x for x in sys.path if 'site-packages' in x]
finder=modulefinder.ModuleFinder(searchpaths)
finder.run_script(sourcepath)
print(finder.report())
modules=[]
badmodules=finder.badmodules.keys()
for name, mod in finder.modules.iteritems():
path=mod.__file__
# Ignore internal modules
if path is None: continue
# Ignore DLL internal modules
#if '\\DLLs\\' in path: continue
# Watch out for C modules
if os.path.splitext(path)[1]=='.pyd':
print("WARNING: I don't support handling C modules at '"+path+"'")
badmodules.append(name)
continue
modules.append((name, os.path.abspath(path)))
modules.sort()
print("Modules not imported due to not found, error or being a C module:")
print("\n".join(badmodules))
raw_input("\nPress Return if you are happy with these missing modules ...")
with open(destpath+os.sep+"files.txt", "w") as oh:
oh.writelines([x[1]+'\n' for x in modules])
cmd='ipy64 '+destpath+os.sep+'pyc.py /main:"'+os.path.abspath(sourcepath)+'" /out:'+os.path.splitext(os.path.basename(sourcepath))[0]+' /target:exe /standalone /platform:x86 /files:'+destpath+os.sep+'files.txt'
print(cmd)
cwd=os.getcwd()
try:
os.chdir(destpath)
retcode=subprocess.call(cmd, shell=True)
finally:
os.chdir(cwd)
sys.exit(retcode)
Run Code Online (Sandbox Code Playgroud)
这是使用其新的独立二进制功能针对IronPython v2.7.2 RC1编写的,实际上它确实有效.你得到一个完全独立的独立.exe文件 - 它不需要安装任何其他东西.该脚本的工作原理是解析所提供脚本的导入并将整个批次发送到pyc.py. 这是个好消息.
坏消息如下:
你去吧 它可行,但解决方案仍需要更多成熟.祝你好运!