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.

Method 1
Method 1 of 2:

Using CX_Freeze

  1. 1
    Download cx_Freeze from Sourceforge. It's a tool for packaging Python scripts into standalone executables.
  2. 2
    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.
    Advertisement
  3. 3
    Create a new Python file named setup.py in the directory of the Python program you wish to compile.
  4. 4
    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
             )
      
  5. 5
    Run the following commands in your computer's terminal:
       cd [path to your Python file's directory]
       python setup.py build
      
  6. 6
    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.
  7. Advertisement
Method 2
Method 2 of 2:

Using PyInstaller

  1. 1
    Open terminal or command prompt and execute the following code. This will install pyInstaller.
      		 pip install pyinstaller
      
  2. 2
    Open the directory where the python script is located. On Windows "Right Click" while holding "Shift" and select "open command window here". On linux "Right Click" and select "Open Terminal".
  3. 3
    Type this command to compile your script. Wait for the command to finish.
      		 pyInstaller script_name.py
      
  4. 4
    Move into the newly created "dist" directory. Your compiled project will be there.
  5. Advertisement

Community Q&A

  • Question
    How can I develop a Linux OS easily?
    Community Answer
    Community Answer
    There's no easy way to develop a Linux OS. You could try making a spin of another distro, but that's still hard. You'll need lots of experience to make one from scratch.
Advertisement

Warnings

  • Setting base = 'Win32GUI' as shown above will cause problems if your Python code includes the input() function.
    ⧼thumbs_response⧽
  • Try setting base='Console', or base=None since Console is the default.
    ⧼thumbs_response⧽
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 9 people, some anonymous, worked to edit and improve it over time. This article has been viewed 59,016 times.
How helpful is this?
Co-authors: 9
Updated: January 19, 2020
Views: 59,016
Categories: Python
Advertisement