我正在构建一个Python应用程序,不想强迫我的客户端安装Python和模块。
那么,是否有一种方法可以将Python脚本编译为独立的可执行文件?
我正在构建一个Python应用程序,不想强迫我的客户端安装Python和模块。
那么,是否有一种方法可以将Python脚本编译为独立的可执行文件?
当前回答
pyinstaller yourfile.py -F --onefile
这将在Windows上创建一个独立的EXE文件。
重要提示1:EXE文件将生成在名为“dist”的文件夹中。
重要提示2:不要忘记—onefile标志
您可以使用pip install PyInstaller安装PyInstaller
注意:在极少数情况下,存在隐藏的依赖关系…所以如果你运行EXE文件并得到丢失库错误(下面的例子中是win32timezone),然后使用这样的方法:
pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
其他回答
使用py2exe……使用下面的设置文件:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
setup(
console = ['PlotMemInfo.py'],
options = {
'py2exe': {
'packages': ['matplotlib'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
},
data_files = matplotlib.get_py2exe_datafiles()
)
使用PyInstaller,我发现了一个更好的方法,使用快捷方式到.exe,而不是制作——onefile。无论如何,可能会有一些数据文件,如果你运行的是基于站点的应用程序,那么你的程序也依赖于HTML, JavaScript和CSS文件。把这些文件都搬到别的地方去没有任何意义。相反,如果我们向上移动工作路径呢?
为EXE文件创建一个快捷方式,将其移动到顶部,并设置目标和起始路径,以使相对路径进入dist\文件夹:
Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe
Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\"
我们可以将快捷方式重命名为任何东西,所以重命名为“GTFS-Manager”。 现在,当我双击快捷方式时,就好像我用python运行了这个文件!我发现这种方法比——onefile更好,因为:
In onefile's case, there's a problem with a .dll missing for the Windows 7 OS which needs some prior installation, etc. Yawn. With the usual build with multiple files, no such issues. All the files that my Python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top. I can actually use this exact same folder on Ubuntu (run python3 myfile.py) and Windows (double-click the shortcut). I don't need to bother with the overly complicated hacking of .spec file to include data files, etc.
哦,记得在构建后删除构建文件夹。它将节省尺寸。
pyinstaller yourfile.py -F --onefile
这将在Windows上创建一个独立的EXE文件。
重要提示1:EXE文件将生成在名为“dist”的文件夹中。
重要提示2:不要忘记—onefile标志
您可以使用pip install PyInstaller安装PyInstaller
注意:在极少数情况下,存在隐藏的依赖关系…所以如果你运行EXE文件并得到丢失库错误(下面的例子中是win32timezone),然后使用这样的方法:
pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
是的,可以将Python脚本编译成独立的可执行文件。
PyInstaller可用于将Python程序转换为独立的可执行文件,适用于Windows、Linux、Mac OS X、FreeBSD、Solaris和AIX。它是推荐的转换器之一。
py2exe将Python脚本转换为Windows平台上的可执行文件。
Cython是Python编程语言和扩展的Cython编程语言的静态编译器。
使用Cython转换为C、编译并链接GCC。
另一种方法是,用C语言编写核心函数(那些你想要使其难以反转的函数),编译它们并使用Boost。Python导入编译后的代码(而且代码执行速度更快)。然后使用上面提到的任何工具进行分发。