How to Compile Python Script

Method 1 of 2:

Using CX_Freeze

  1. How to Compile Python Script Picture 1
    Download cx_Freeze from Sourceforge. It's a tool for packaging Python scripts into standalone executables.
  2. How to Compile Python Script Picture 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.
  3. How to Compile Python Script Picture 3
    Create a new Python file named setup.py in the directory of the Python program you wish to compile.
  4. How to Compile Python Script Picture 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. How to Compile Python Script Picture 5
    Run the following commands in your computer's terminal:
     cd [path to your Python file's directory] python setup.py build 
  6. How to Compile Python Script Picture 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.
    1. 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.
    2. The build can be customized in many ways. See cx-freeze.readthedocs.org for a description of all possible options.
Method 2 of 2:

Using PyInstaller

  1. How to Compile Python Script Picture 7
    Open terminal or command prompt and execute the following code. This will install pyInstaller.
     pip install pyinstaller 
  2. How to Compile Python Script Picture 8
    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. How to Compile Python Script Picture 9
    Type this command to compile your script. Wait for the command to finish.
     pyInstaller script_name.py 
  4. How to Compile Python Script Picture 10
    How to Compile Python Script Picture 11
    Move into the newly created "dist" directory. Your compiled project will be there.
5 ★ | 1 Vote

May be interested

  • How to Install TkinterPhoto of How to Install Tkinter
    tkinter (tk) is a python default gui and comes with the python installation on linux, mac, and windows. since tk comes with most python installations, you don't generally need to install it yourself. since python 2 and python 3 vary so...
  • How to Start Programming in PythonPhoto of How to Start Programming in Python
    do you want to start learning how to program? getting into computer programming can be daunting, and you may think that you need to take classes in order to learn. while that may be true for some languages, there are a variety of...
  • How to Check Python Version on PC or MacPhoto of How to Check Python Version on PC or Mac
    this wikihow teaches you how to find which version of python is installed on your windows or macos computer. open windows search. if you don't already see a search box in the taskbar, click the magnifying glass or circle next to , or press...
  • How to Uninstall PythonPhoto of How to Uninstall Python
    this wikihow teaches you how to remove the python application and its related files and folders from your computer. you can do this on both windows and mac computers. open start . click the windows logo in the bottom-left corner of the...
  • The oct () function in PythonPhoto of The oct () function in Python
    the oct () function is one of python's built-in functions, which is used to convert an integer to the corresponding octal.
  • The reversed () function in PythonPhoto of The reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.