PyInstaller - How to Turn Your Python Code into an Exe on Windows - Mouse Vs Python (2023)

You have just created an awesome new application. Maybe it’s a game or maybe it’s an image viewer. Whatever your application is, you want to share it with your friend or a family member. However, you know they won’t know how to install Python or any of the dependencies. What do you do? You need something that will transform your code into an executable!

Python has many different tools you can use to convert your Python code into a Windows executable. Here are a few different tools you can use:

  • PyInstaller
  • py2exe
  • cx_freeze
  • Nuitka
  • Briefcase

These various tools can all be used to create executables for Windows. They work slightly differently, but the end result is that you will have an executable and perhaps some other files that you need to distribute too.

PyInstaller and Briefcase can be used to create Windows and MacOS executables. Nuitka is a little different in that it turns your Python code into C code before converting it into an executable. What this means is that the result ends up much smaller than PyInstaller’s executable.

However, for this article, you will focus on PyInstaller. It is one of the most popular packages for this purpose and has a lot of support. PyInstaller also has good documentation and there are many tutorials available for it.

(Video) Python 101: Episode #43 Creating Executables with PyInstaller

In this article, you will learn about:

  • Installing PyInstaller
  • Creating an Executable for a Command-Line Application
  • Creating an Executable for a GUI

Let’s transform some code into a Windows executable!

Installing PyInstaller

To get started, you will need to install PyInstaller. Fortunately, PyInstaller is a Python package that can be easily installed using pip:

python -m pip install pyinstaller

This command will install PyInstaller and any dependencies that it needs on your machine. You should now be ready to create an executable with PyInstaller!

Creating an Executable for a Command-Line Application

The next step is to pick some code that you want to turn into an executable. You can use the PySearch utility from chapter 32 of my book, Python 101: 2nd Edition, and turn it into a binary. Here is the code:

(Video) [OLD] Converting a Python backdoor to Exe using PyInstaller + Basic AVEvasion testing

# pysearch.pyimport argparseimport pathlibdef search_folder(path, extension, file_size=None): """ Search folder for files """ folder = pathlib.Path(path) files = list(folder.rglob(f'*.{extension}')) if not files: print(f'No files found with {extension=}') return if file_size is not None: files = [f for f in files if f.stat().st_size > file_size] print(f'{len(files)} *.{extension} files found:') for file_path in files: print(file_path)def main(): parser = argparse.ArgumentParser( 'PySearch', description='PySearch - The Python Powered File Searcher') parser.add_argument('-p', '--path', help='The path to search for files', required=True, dest='path') parser.add_argument('-e', '--ext', help='The extension to search for', required=True, dest='extension') parser.add_argument('-s', '--size', help='The file size to filter on in bytes', type=int, dest='size', default=None) args = parser.parse_args() search_folder(args.path, args.extension, args.size)if __name__ == '__main__': main()

Next, open up a Command Prompt (cmd.exe) in Windows and navigate to the folder that has your file in it. To turn the Python code into a binary executable, you need to run the following command:


If Python isn’t on your Windows path, you may need to type out the full path to pyinstaller to get it to run. It will be located in a Scripts folder wherever your Python is installed on your system.

When you run that command, you will see some output that will look similar to the following:

6531 INFO: PyInstaller: 3.66576 INFO: Python: 3.8.26707 INFO: Platform: Windows-10-10.0.10586-SP06828 INFO: wrote C:\Users\mike\AppData\Local\Programs\Python\Python38-32\pysearch.spec6880 INFO: UPX is not available.7110 INFO: Extending PYTHONPATH with paths['C:\\Users\\mike\\AppData\\Local\\Programs\\Python\\Python38-32', 'C:\\Users\\mike\\AppData\\Local\\Programs\\Python\\Python38-32']7120 INFO: checking Analysis7124 INFO: Building Analysis because Analysis-00.toc is non existent7128 INFO: Initializing module dependency graph...7153 INFO: Caching module graph hooks...7172 INFO: Analyzing ...

PyInstaller is very verbose and will print out a LOT of output. When it is finished, you will have a dist folder with a pysearch folder inside of it. Within the pysearch folder are many other files, including one called pysearch.exe. You can try navigating to the pysearch folder in your Command Prompt and then run pysearch.exe:

C:\Users\mike\AppData\Local\Programs\Python\Python38-32\dist\pysearch>pysearch.exeusage: PySearch [-h] -p PATH -e EXTENSION [-s SIZE]PySearch: error: the following arguments are required: -p/--path, -e/--ext

That looks like a pretty successful build! However, if you want to give the executable to your friends, you will have to give them the entire pysearch folder as all those other files in there are also required.

(Video) Convert PY to EXE Automatically

You can fix that issue by passing the --onefile flag, like this:

pyinstaller --onefile

The output from that command is similar to the first command. This time when you go into the dist folder though, you will find a single file in there called pysearch.exe instead of a folder full of files.

Creating an Executable for a GUI

Creating an executable for a GUI is slightly different than it is for a command-line application. The reason is that the GUI is the main interface and PyInstaller’s default is that the user will be using a Command Prompt or console window. If you run either of the PyInstaller commands that you learned about in the previous section, it will successfully create your executable. However, when you go to use your executable, you will see a Command Prompt appear in addition to your GUI.

You usually don’t want that. To suppress the Command Prompt, you need to use the --noconsole flag.

To test out how this would work, grab the code for the image viewer that was created with wxPython from chapter 42 of Python 101: 2nd Edition. Here is the code again for your convenience:

(Video) How to Convert PY to EXE | How to Convert Python to EXE | Pyinstaller Tutorial

# image_viewer.pyimport wxclass ImagePanel(wx.Panel): def __init__(self, parent, image_size): super().__init__(parent) self.max_size = 240 img = wx.Image(*image_size) self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.Bitmap(img)) browse_btn = wx.Button(self, label='Browse') browse_btn.Bind(wx.EVT_BUTTON, self.on_browse) self.photo_txt = wx.TextCtrl(self, size=(200, -1)) main_sizer = wx.BoxSizer(wx.VERTICAL) hsizer = wx.BoxSizer(wx.HORIZONTAL) main_sizer.Add(self.image_ctrl, 0, wx.ALL, 5) hsizer.Add(browse_btn, 0, wx.ALL, 5) hsizer.Add(self.photo_txt, 0, wx.ALL, 5) main_sizer.Add(hsizer, 0, wx.ALL, 5) self.SetSizer(main_sizer) main_sizer.Fit(parent) self.Layout() def on_browse(self, event): """ Browse for an image file @param event: The event object """ wildcard = "JPEG files (*.jpg)|*.jpg" with wx.FileDialog(None, "Choose a file", wildcard=wildcard, style=wx.ID_OPEN) as dialog: if dialog.ShowModal() == wx.ID_OK: self.photo_txt.SetValue(dialog.GetPath()) self.load_image() def load_image(self): """ Load the image and display it to the user """ filepath = self.photo_txt.GetValue() img = wx.Image(filepath, wx.BITMAP_TYPE_ANY) # scale the image, preserving the aspect ratio W = img.GetWidth() H = img.GetHeight() if W > H: NewW = self.max_size NewH = self.max_size * H / W else: NewH = self.max_size NewW = self.max_size * W / H img = img.Scale(NewW,NewH) self.image_ctrl.SetBitmap(wx.Bitmap(img)) self.Refresh()class MainFrame(wx.Frame): def __init__(self): super().__init__(None, title='Image Viewer') panel = ImagePanel(self, image_size=(240,240)) self.Show()if __name__ == '__main__': app = wx.App(redirect=False) frame = MainFrame() app.MainLoop()

To turn this into an executable, you would run the following PyInstaller command:

pyinstaller.exe --noconsole

Note that you are not using the --onefile flag here. Windows Defender will flag GUIs that are created with the --onefile as malware and remove it. You can get around that by not using the --onefile flag or by digitally signing the executable. Starting in Windows 10, all GUI applications need to be signed or they are considered malware.

Microsoft has a Sign Tool you can use, but you will need to purchase a digital certificate or create a self-signed certificate with Makecert, a .NET tool or something similar.

Wrapping Up

There are lots of different ways to create an executable with Python. In this article, you used PyInstaller. You learned about the following topics:

  • Installing PyInstaller
  • Creating an Executable for a Command-Line Application
  • Creating an Executable for a GUI

PyInstaller has many other flags that you can use to modify its behavior when generating executables. If you run into issues with PyInstaller, there is a mailing list that you can turn to. Or you can search with Google and on StackOverflow. Most of the common issues that crop up are covered either in the PyInstaller documentation or are easily discoverable through searching online.

(Video) Standalone Python EXE Executable - Python Kivy GUI Tutorial #20

PyInstaller - How to Turn Your Python Code into an Exe on Windows - Mouse Vs Python (1)Would you like to learn more about Python?

Python 101 – 2nd Edition

Purchase now on Leanpub or Amazon


How to convert Python code to exe using PyInstaller? ›

Steps to Create an Executable using PyInstaller
  1. Step 1: Add Python to Windows Path. To start, you may want to add Python to Windows path. ...
  2. Step 2: Install the PyInstaller Package. ...
  3. Step 3: Save your Python Script. ...
  4. Step 4: Create the Executable using PyInstaller. ...
  5. Step 5: Run the Executable.

How to convert py to exe using auto py to exe? ›

Creating an . EXE file with Auto Py To Exe
  1. In your terminal, navigate to the auto_py_to_exe directory.
  2. Activate the virtual environment.
  3. Install the requirements using pip install -r requirements.txt.
  4. Run the following command: auto-py-to-exe.

Can Python code be converted to exe? ›

Pyinstaller allows you to quickly convert a Python file to an executable file from your terminal.


1. Python 101: Episode #40 - Creating executables with py2exe
(Mouse Vs Python)
2. Python keylogger bypasses Windows 11 Defender // Convert WIFI py to EXE
(David Bombal)
3. How to Convert Python Script to .EXE using Pyinstaller
4. Python 101: Episode #42 - Creating Executables with cx_Freeze
(Mouse Vs Python)
5. How to compile Python scripts with PyInstaller
(Ptrace Security GmbH)
6. How To Make a Python Program Executable in Linux
(matty tripps)


Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated: 02/09/2023

Views: 5947

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.