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

How to Create a Single Executable from a Python Script using py2exe?

Linda Hamilton
Linda HamiltonOriginal
2024-11-30 16:10:10287browse

How to Create a Single Executable from a Python Script using py2exe?

Creating a Single Executable File with py2exe

py2exe is an invaluable tool that allows developers to convert Python scripts into standalone Windows executables. It offers a unique feature known as "bundle_files" that enables the generation of a single executable file, eliminating the need for extracting and running multiple files.

The "bundle_files" Option

The "bundle_files" option in py2exe's setup.py configuration file controls whether and how the distribution files are bundled with the executable. Setting it to 1 will bundle everything, including the Python interpreter.

Setting up the Executable

To create a single-file executable, follow these steps:

  • Set bundle_files to 1: Indicate that everything should be bundled into the executable.
  • Set compressed to True: Compress the bundled files for efficient distribution.
  • Set zipfile to None: Prevent the creation of an additional library.zip file.

Sample Setup.py

Here is a sample setup.py file that demonstrates these settings:

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

By implementing these settings, you can use py2exe to effortlessly generate a single executable file that will run your Python script.

The above is the detailed content of How to Create a Single Executable 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