Home >Backend Development >Python Tutorial >How Can I Create a Single Executable File from a Python Program Using py2exe?

How Can I Create a Single Executable File from a Python Program Using py2exe?

Barbara Streisand
Barbara StreisandOriginal
2024-12-14 04:16:12990browse

How Can I Create a Single Executable File from a Python Program Using py2exe?

Single Executable File with py2exe

py2exe offers a method to generate a single executable file for a Python program. To accomplish this, the bundle_files option should be utilized in the setup.py file.

Setup.py Configuration

For a single executable, set bundle_files to 1, compressed to True, and zipfile to None. This generates a compressed executable without the need for file extraction.

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,
)

Explanation of Options

  • bundle_files:

    • 3 (default): Don't bundle
    • 2: Bundle everything except the Python interpreter
    • 1: Bundle everything, including the Python interpreter
  • compressed: Whether to compress the bundled files
  • zipfile: By setting this to None, the bundled files will be included directly within the executable rather than in a separate library.zip file.

Example

This setup.py file will generate a single executable file from the single.py script:

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,
)

The above is the detailed content of How Can I Create a Single Executable File from a Python Program 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