How to Compile Python Script

October 2022 · 2 minute read

Python is a very popular language for programming. But what if the person running your program does not want or know how to run a Python script? This article will teach you how to compile a Python script into an executable. Download...

Method 1 of 2:

Using CX_Freeze

  • Picture 1 of How to Compile Python Script

    Download cx_Freeze from Sourceforge. It's a tool for packaging Python scripts into standalone executables.

  • Picture 2 of How to Compile Python Script

    Make sure you are working on the platform you need your executable to run on. For example, if you want to create a Windows executable file, run cx_Freeze on Windows. Same goes for Mac and Linux.

  • Picture 3 of How to Compile Python Script

    Create a new Python file named setup.py in the directory of the Python program you wish to compile.

  • Picture 4 of How to Compile Python Script Enter the following code into your new setup.py file. (As always in Python, correct indentation is important, and unfortunately is not shown here due to formatting difficulties.):
    import sys from cx_Freeze import setup, Executable base = None if sys.platform == 'win32': base = 'Win32GUI' executables = [ Executable(Python program name, base=base) ] setup(name=executable_name, version='version', description='desc', executables=executables ) 
  • Picture 5 of How to Compile Python Script Run the following commands in your computer's terminal:
     cd [path to your Python file's directory] python setup.py build 
  • Picture 6 of How to Compile Python Script Look for a new folder called "build" in the Python program's directory. It should have been created during the previous step. Open that folder and the folder inside it.
  • There's your executable! The other files in that directory are required to run your executable, so be sure to always keep them with the executable.
  • The build can be customized in many ways. See cx-freeze.readthedocs.org for a description of all possible options.
  • Jessica Tanner Jessica Tanner

    Update 05 March 2020

    ncG1vNJzZmismaXArq3KnmWcp51ktbDDjK2mZpufor2quMRmp7KsmKS7br%2FCq6CprA%3D%3D