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

How Can py2exe Create a Single Executable File from a Python Script?

Susan Sarandon
Susan SarandonOriginal
2024-12-01 01:01:11957browse

How Can py2exe Create a Single Executable File from a Python Script?

Generating Single Executable Files with py2exe

Creating a single executable file from a Python script can simplify distribution and ease of use for users. The py2exe package provides a way to achieve this.

To generate a single executable, follow these steps:

  1. Set the Bundle Files Option: In your setup.py file, include the bundle_files option with a value of 1. This tells py2exe to bundle all script and library files into the executable.
  2. Enable Compression: To reduce the size of the executable, set the compressed option to True. This will compress the bundled files.
  3. Disable Zip File Creation: Since we're bundling files directly into the executable, set the zipfile option to None. This prevents py2exe from creating a separate ZIP file containing the bundled files.

Here's an example setup.py file demonstrating these settings:

from distutils.core import setup
import py2exe

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

Once you've updated your setup.py file, run the following command to generate the single executable:

python setup.py py2exe

The resulting executable will be located in the dist folder. It will contain all the necessary Python libraries and scripts, making it a single self-contained file ready for distribution.

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