How to Make Python Script Executable

Written by wolfram donat
  • Share
  • Tweet
  • Share
  • Email

One reason for Python's increased popularity as a high-level programming language is its portability. Scripts written on a Mac, for example, will run on a Windows machine and vice versa. As long as the destination machine has Python installed on it, the script will run without problems. Most Python scripts are saved as scripts; they are not self-contained, and they are normally executed by typing "python xxx.py" in a command prompt. However, this method of running the scripts requires a Python installation. It is possible to make a Python script executable -- with no Python installation required -- by turning it into a "frozen binary."

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Download and install a frozen binary system. Three systems are commonly used. Py2exe (for Windows) is available at www.py2exe.org. Py2app (a Mac version of Py2exe) is available at svn.pythonmac.org/py2app. PyInstaller (for Windows, Mac and Linux) is available at www.pyinstaller.org.

    To install py2exe, download the .ZIP file and unpack it. No installation is required.

    To install py2app, download the .TAR.GZ file from Python.org and unpack it. Open a terminal and navigate to the directory in which you unpacked the files. Type:

    python setup.py install

    and the program will install itself.

    To install PyInstaller, download the .ZIP file from the main page and unpack it. Open a terminal or command line and navigate to the unpacked contents directory. Type

    python Configure.py

    and the program will install and configure itself.

  2. 2

    Write your Python script that you wish to save as an executable. For example:

    print "Hello, world!"

    can be saved as hello.py.

  3. 3

    Use Py2exe or Py2app to create an executable. Because Py2app is the Mac version of Py2exe, the sequences to use them are similar. Both require a setup.py script. In Py2exe:

    from distutils.core import set-up

    import py2exe

    set-up(console=['hello.py'])

    will import the setup function from distutils, import the py2exe module, and then setup the console application hello.py.

    In Py2app, you can use the Py2applet script to create the setup.py:

    py2applet --make-setup hello.py

    This creates setup.py.

  4. 4

    Run your set-up script:

    Python setup.py Py2exe

    This will create the hello.exe file, which is a self-contained executable.

    For Py2app, type

    python setup.py py2app -A

    This will create the hello.app file, which is a self-contained app file that can be run from the terminal.

  5. 5

    Create a spec file in PyInstaller by navigating to your script folder and typing

    python Makespec.py [--onefile] hello.py

    Build your project with:

    python Build.py specfile

    This creates a standalone .EXE file.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.