如何将python脚本编译为二进制executab

2024-05-01 22:02:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将Python脚本转换为Windows可执行文件。

我已将Python 2.6安装到python26

我已经创建了一个脚本并将其保存在C:\pythonscript。在这个文件夹里有两个文件

Setup.pyoldlogs.py(此文件需要封面)

setup.py代码是

from distutils.core import setup
import py2exe

setup(console=['oldlogs.py'])

如何将oldlogs.py转换为exe文件?


Tags: 文件代码frompyimport脚本文件夹可执行文件
3条回答
# -*- mode: python -*-

block_cipher = None

a = Analysis(['SCRIPT.py'],
             pathex=[
                 'folder path',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_50c6cb8431e7428f',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_c4f50889467f081d'
             ],
             binaries=[(''C:\\Users\\chromedriver.exe'')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='NAME OF YOUR EXE',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

我建议使用PyInstaller,可以使用以下命令将简单的python脚本转换为exe:

utils/Makespec.py [--onefile] oldlogs.py

它创建一个yourprogram.spec文件,该文件是用于生成最终exe的配置。下一个命令从配置文件生成exe:

utils/Build.py oldlogs.spec

可以找到更多here

或者使用PyInstaller替代py2exe。这是一个很好的starting point。PyInstaller让我们为linux和mac创建可执行文件。。。

下面是如何相当容易地使用PyInstaller来解决手头的问题:

pyinstaller oldlogs.py

从工具的文档中:

PyInstaller analyzes myscript.py and:

  • Writes myscript.spec in the same folder as the script.
  • Creates a folder build in the same folder as the script if it does not exist.
  • Writes some log files and working files in the build folder.
  • Creates a folder dist in the same folder as the script if it does not exist.
  • Writes the myscript executable folder in the dist folder.

In the dist folder you find the bundled app you distribute to your users.

相关问题 更多 >