Python的项目中,如果想要在其他的电脑上运行你的代码,那么相对于C/C#等静态语言来说,可能并不是这么容易。因为动态语言运行需要解释器。这就意味着其他电脑上必须要有和你的开发环境相同的解释器和运行环境。那么有没有办法像C/C#那样一次编译到处使用呢?
答案是肯定的,以IronPython 2.7为例:官方提供了一个pyc.py的工具,可以在:IronPython 2.7\Tools\Scripts路径下找到。使用它可以将我们的代码打包为.exe可执行文件,无论是纯脚本还是带有UI交互窗口的项目,都能打包,使用方法如下:
可以在文件pyc.py的开头找到
pyc: The Command-Line Python Compiler
Usage: ipy.exe pyc.py [options] file [file ...]
Options:
/out:output_file Output file name (default is main_file.<extenstion>)
/target:dll Compile only into dll. Default
/target:exe Generate console executable stub for startup in addition to dll.
/target:winexe Generate windows executable stub for startup in addition to dll.
@<file> Specifies a response file