Ironpython 2.6 .py - > .exe

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.dlltest.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.


Nia*_*las 6

这是一个长期存在的问题,关于互联网上的信息很少.我能找到的唯一已知解决方案是使用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. 这是个好消息.

坏消息如下:

  1. IronPython v2.7.2 RC1的ModuleFinder似乎不起作用,因此需要使用CPython运行上述脚本.然后使用CPython的ModuleFinder,但使用IronPython的自定义运行时库.是的,我很惊讶它也有效......
  2. 二进制文件输出开始于大约8Mb.一个简单的单元测试在16Mb时称重.有很多东西不需要在那里,例如它抛出Wpf支持和更多,但它们仍然不小.
  3. 加载时间比非独立加载时间慢得多.考虑在快速英特尔酷睿2上进行独立单元测试需要40秒,而非独立测试则需要大约3秒.如果仅为x86编译,那将下降到十秒.
  4. 运行时性能比非独立运行时慢约40%.如果仅为x86编译,性能大约翻倍.这就是我离开/ platform:x86的原因.
  5. 在CPython的编码和编解码器支持中存在一个众所周知的错误,其中ModuleFinder根本不包含任何编解码器支持,除非您手动指定它.因此,例如,如果您使用UTF-8和codecs.open(),那么您需要"从编码导入utf_8作为some_unique_identifier"来强制依赖.
  6. 以上假设修改了pyc.py,它可以采用/ files参数,因为命令行长度限制很容易被超出.您可以简单地修改自己的pyc.py,如果没有,我已经提交了包含在下一个IronPython中的增强功能.

你去吧 它可行,但解决方案仍需要更多成熟.祝你好运!