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

  • Manage files and folders in PythonManage files and folders in Python
    python also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
  • Multiple choice quiz about Python - Part 3Multiple choice quiz about Python - Part 3
    today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
  • 5 choose the best Python IDE for you5 choose the best Python IDE for you
    in order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
  • What is Python? Why choose Python?What is Python?  Why choose Python?
    python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages ​​for people who are first exposed to programming languages.
  • Module time in PythonModule time in Python
    python has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!
  • Python data type: string, number, list, tuple, set and dictionaryPython data type: string, number, list, tuple, set and dictionary
    in this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
  • How to install Python on Windows, macOS, LinuxHow to install Python on Windows, macOS, Linux
    to get started with python, you first need to install python on the computer you are using, be it windows, macos or linux. below is a guide to installing python on your computer, specific to each operating system.
  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • Multiple choice quiz about Python - Part 4Multiple choice quiz about Python - Part 4
    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
  • How to use Closure in PythonHow to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!