Home >Backend Development >Python Tutorial >How to Create a Single Executable File from a Python Script Using py2exe?

How to Create a Single Executable File from a Python Script Using py2exe?

Susan Sarandon
Susan SarandonOriginal
2024-11-29 19:32:16391browse

How to Create a Single Executable File from a Python Script Using py2exe?

How to Generate Single Executable File with py2exe

In computing, a single executable file is a program that can be directly executed without the need for additional dependencies or installation. It contains all the necessary code and resources to run the program. One way to generate a single executable file in Python is through py2exe.

Using py2exe's bundle_files Option

To create a single executable file using py2exe, utilize the bundle_files option in your setup.py file. Set bundle_files to 1, specify compressed as True, and set zipfile to None. This instructs py2exe to bundle all files, including the Python interpreter, into a compressed single file for distribution.

As explained by py2exe documentation, valid values for bundle_files include:

  • 3 (default): Don't bundle files
  • 2: Bundle everything except the Python interpreter
  • 1: Bundle everything, including the Python interpreter

By setting zipfile to None, the bundled files are incorporated into the executable instead of a separate library.zip archive.

Sample Setup.py File

Here's a sample setup.py file demonstrating the bundle_files configuration:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows = [{'script': "single.py"}],
    zipfile = None,
)

When executed, this setup.py file will generate a single executable file (single.exe) that includes the Python interpreter and all necessary files to run the single.py script.

The above is the detailed content of How to Create a Single Executable File from a Python Script Using py2exe?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn