我已经尝试使用了py2exe(与ipy不兼容)和PYC(已过时)。 有人能指点我一个好的编译器吗?
我已经尝试使用了py2exe(与ipy不兼容)和PYC(已过时)。 有人能指点我一个好的编译器吗?
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
/target:winexe
而不是/target:exe
。test.dll
和test.exe
。test.dll
将包含您的实际脚本代码,而test.exe
只是test.dll
的启动器。如果您包含了这些文件,您可以将此EXE和DLL分发到其他没有安装IronPython的计算机上。所以这就是我的解决方案,我现在告诉你它没有被发布为一个正式的项目,原因很简单:
#!/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)
C:\Program Files
中的IronPython文件夹中。ipy.exe pyc.py other_hw.py /main:console_hw.py
但是它给了我这个错误:Traceback (most recent call last):
File "pyc\pyc.py", line 35, in pyc\pyc.py
AttributeError: attribute 'CompilerSink' of 'namespace#' object is read-only
class PycSink(Hosting.CompilerSink):
之后:class PycSink():
由于权限问题,保存文件成为了一个问题。所以我将pyc.py的内容复制到一个新的IDLE窗口中(创建副本),删除了现有的 pyc.py
副本,并将副本另存为相同位置下的 pyc.py
。这解决了权限问题并允许进行更改。ipy.exe pyc.py other_hw.py /main:console_hw.py
但是,这一次,我收到了以下错误:Traceback (most recent call last):
File "pyc\pyc.py", line 170, in pyc\pyc.py
File "pyc\pyc.py", line 56, in Main
AttributeError: attribute 'ResourceFile' of 'namespace#' object is read-only
此时,我意识到现在已经是凌晨1点,而且明天还有期中考试,所以我撤销了更改并关闭了它。
如果您有解决方案或对我的方案有任何进展,请告诉我。
是的,我也发现编译exe太难了,所以我又回到使用标准Python了。他们应该在IronPython网站上提供一个好的教程。