如何将python脚本编译为二进制可执行文件

问题:如何将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文件?

I need to convert a Python script to a Windows executable.

I have Python 2.6 installed to python26.

I have created one script and kept it in C:\pythonscript. Inside this folder there are two files

Setup.py and oldlogs.py (this file need coversion)

setup.py code is

from distutils.core import setup
import py2exe

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

How can I convert oldlogs.py to an exe file?


回答 0

或者使用PyInstaller替代py2exe。这是一个很好的起点。PyInstaller还可以让您为Linux和Mac创建可执行文件。

这是一个相当容易使用PyInstaller解决当前问题的方法:

pyinstaller oldlogs.py

从该工具的文档中:

PyInstaller分析myscript.py并执行以下操作:

  • 在脚本所在的文件夹中写入myscript.spec。
  • 如果脚本不存在,则在与脚本相同的文件夹中创建文件夹。
  • 在构建文件夹中写入一些日志文件和工作文件。
  • 如果脚本不存在,则在与脚本相同的文件夹中创建文件夹dist。
  • 将myscript可执行文件文件夹写入dist文件夹。

在dist文件夹中,您找到分发给用户的捆绑应用程序。

Or use PyInstaller as an alternative to py2exe. Here is a good starting point. PyInstaller also lets you create executables for linux and mac…

Here is how one could fairly easily use PyInstaller to solve the issue at hand:

pyinstaller oldlogs.py

From the tool’s documentation:

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.


回答 1

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

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

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

utils/Build.py oldlogs.spec

这里可以找到更多

I recommend PyInstaller, a simple python script can be converted to an exe with the following commands:

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

which creates a yourprogram.spec file which is a configuration for building the final exe. Next command builds the exe from the configuration file:

utils/Build.py oldlogs.spec

More can be found here


回答 2

# -*- 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 )
# -*- 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 )