我正在构建一个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。您还可以在其中找到在Linux上执行此操作的信息。
您可以使用PyInstaller将Python程序打包为独立的可执行文件。它可以在Windows、Linux和Mac上运行。
PyInstaller Quickstart Install PyInstaller from PyPI: pip install pyinstaller Go to your program’s directory and run: pyinstaller yourprogram.py This will generate the bundle in a subdirectory called dist. pyinstaller -F yourprogram.py Adding -F (or --onefile) parameter will pack everything into single "exe". pyinstaller -F --paths=<your_path>\Lib\site-packages yourprogram.py running into "ImportError" you might consider side-packages. pip install pynput==1.6.8 still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller For a more detailed walkthrough, see the manual.
我喜欢PyInstaller——尤其是“windowed”版本:
pyinstaller --onefile --windowed myscript.py
它将在distination/文件夹中创建一个*.exe文件。
第三个选项是cx_Freeze,它是跨平台的。
使用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.
哦,记得在构建后删除构建文件夹。它将节省尺寸。