Create Executable of Python Script using PyInstaller – Data to Fish (2024)

In this short guide, you’ll see the how to create an executable of a Python script using PyInstaller.

The following video tutorial is also available:

Steps to Create an Executable using PyInstaller

Step 1: Add Python to Windows Path

To start, you may want to add Python to Windows path.

An easy way to add Python to the path is by downloading a recent version of Python, and then checking the box to ‘Add Python to PATH’ at the beginning of the installation:

Add Python to PATH

Finish the installation, and you should be good to go.

Step 2: Install the PyInstaller Package

Next, open the Windows “Command Prompt” and then type the following command to install the PyInstaller package:

pip install pyinstaller

Step 3: Save your Python Script

Save your Python script at your desired location.

For example, let’s create a simple Python script that displays ‘Hello World!’ when clicking a button:

import tkinter as tkroot = tk.Tk()canvas = tk.Canvas(root, width=300, height=300)canvas.pack()def hello(): label = tk.Label(root, text="Hello World!", fg="blue", font=("Arial", 18, "bold")) canvas.create_window(150, 200, window=label)button = tk.Button(text="Click Me", command=hello, bg="brown", fg="white")canvas.create_window(150, 150, window=button)root.mainloop()

For demonstration purposes, let’s say that the Python script is stored in the following folder:

C:\Users\Ron\Desktop\Test

Where the Python script is called ‘hello‘ and the file extension is ‘.py

Step 4: Create the Executable using PyInstaller

Now you’ll be able to create the executable of the Python script using PyInstaller.

Simply go to the Command Prompt, and then type:

cd, followed by space, and then the location where your Python script is stored

Here is the command for our example:

C:\Users\Ron>cd C:\Users\Ron\Desktop\Test

Press Enter (after you typed the location where the Python script is stored on your computer).

Then, refer to the following template to create the executable:

pyinstaller --onefile python_script_name.py

Since for our example, the python_script_nameis ‘hello‘ (and the file extension is .py), then the command to create the executable is:

pyinstaller --onefile hello.py

Press Enter for the last time.

Note that if you wish to suppress the terminal window that opens with the program, then use the following command instead:

pyinstaller --noconsole python_script_name.py

For our example:

pyinstaller --noconsole hello.py

Step 5: Run the Executable

Your executable will be created at the location that you specified.

For our example, it will be under the same folder where the ‘hello’ script was originally stored:

C:\Users\Ron\Desktop\Test

You’ll notice that few additional files were created at that location.

To find the executable file, open the dist folder. You’ll then see the executable file:

hello

Double click on the file, and you should be able to launch your program.

In our case, once you click on the ‘hello’ executable, you’ll get a display with a single button.

And if you click on that button, you’ll see the following expression:

Hello World!

You can read more about PyInstaller by visiting the PyInstaller manual.

Create Executable of Python Script using PyInstaller – Data to Fish (2024)

FAQs

Create Executable of Python Script using PyInstaller – Data to Fish? ›

Python scripts are not executable in Linux by default. To make your script executable, use the chmod command with the +x option followed by the name of your script. For example, if your script is named my_script.py, you can use the command chmod +x my_script.py.

How to turn a Python script into an executable? ›

To convert a Python script to a standalone executable (.exe) file using Auto PY to EXE, you can follow these steps:
  1. Step 1: Install Auto PY to EXE. ...
  2. Step 2: Run Auto PY to EXE. ...
  3. Step 3: Configure the settings. ...
  4. Step 4: Select the Compilation Mode. ...
  5. Step 5: Click “Convert .py to .exe. ...
  6. Step 6: Find the output.

How do I make a Python script executable from anywhere? ›

To make Python scripts runnable from any location under Windows:
  1. Create directory to put all your python scripts in. ...
  2. Copy all your python scripts into this directory.
  3. Add the path to this directory in Windows "PATH" system variable: ...
  4. Run or restart "Anaconda Prompt"
  5. Type "your_script_name.py"

How do I make a Python script executable in Linux? ›

Python scripts are not executable in Linux by default. To make your script executable, use the chmod command with the +x option followed by the name of your script. For example, if your script is named my_script.py, you can use the command chmod +x my_script.py.

How to convert Python script to exe using py2exe? ›

There are a few simple steps needed to use py2exe once you've installed it:
  1. Create/test your program.
  2. Create your setup script (setup.py)
  3. Run your setup script.
  4. Test your executable.
  5. Providing the Microsoft Visual C runtime DLL. 5.1. Python 2.4 or 2.5. 5.2. Python 2.6, 2.7, 3.0, 3.1. 5.2.1. ...
  6. Build an installer if applicable.

How to use PyInstaller to make exe? ›

How to Use Pyinstaller to Generate an EXE File
  1. Step 1: Install pyinstaller. Make sure Pyinstaller is installed on your system using pip. ...
  2. Step 2: Navigate to Your Python Script. ...
  3. Step 3: Run Pyinstaller. ...
  4. Step 4: Locate the Generated EXE File. ...
  5. Exploring further more options. ...
  6. Packaging Python Code with GUI. ...
  7. Installation: ...
  8. Usage:
Jan 10, 2024

How to execute a Python script? ›

To execute a Python script, first open a terminal, then navigate to the directory where the script is located, and finally, run the script using the 'python' command followed by the script's name. On Linux, consider using python3 to ensure you're using Python 3.

How to make Python script executable without Python? ›

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.

How do I run a Python script in a specific environment? ›

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .

How do I run Python from anywhere? ›

You can also use PythonAnywhere if you are using a work or school computer that does not allow any software into be installed.
  1. Sign up for an account. ...
  2. Writing Your First Program on PythonAnywhere. ...
  3. Using the Linux Shell on PythonAnywhere. ...
  4. Some Cool Hints on the bash console. ...
  5. Editing Files on PythonAnywhere.

How do I make a script executable? ›

Steps to write and execute a script
  1. Open the terminal. Go to the directory where you want to create your script.
  2. Create a file with . sh extension.
  3. Write the script in the file using an editor.
  4. Make the script executable with command chmod +x <fileName>.
  5. Run the script using ./<fileName>.

What is the command to make a script executable? ›

Make a Bash Script Executable
  1. 1) Create a new text file with a . sh extension. ...
  2. 2) Add #!/bin/bash to the top of it. This is necessary for the “make it executable” part.
  3. 3) Add lines that you'd normally type at the command line. ...
  4. 4) At the command line, run chmod u+x YourScriptFileName.sh. ...
  5. 5) Run it whenever you need!

How to use PyInstaller in cmd? ›

How do I create an EXE using PyInstaller
  1. Create a python sample file named example.py. ...
  2. Create a folder as c:\app, and copy example.py(attached) to c:\app.
  3. Open your command prompt and run pyinstaller example.py command. ...
  4. Copy the jars(aspose-cells-xxx. ...
  5. Edit the file with the spec suffix to add datas section like example.

Which is better, PyInstaller or py2exe? ›

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.

How do I run a Python executable from command prompt? ›

To run Python scripts with the python command, you need to open a command-line window and type in the word python followed by the path to your target script: Windows.

How do I run a Python executable from the command line? ›

To run Python scripts with the python command, you need to open a command-line window and type in the word python followed by the path to your target script: Windows. Linux + macOS.

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6129

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.