How to Convert Python Code into an .exe File? A Step-by-Step Guide to Packaging into a Small Application!

Hello everyone, I am Puff. Today, we won’t discuss complex algorithms or delve into profound theories; instead, let’s talk about a particularly practical and cool feature—how to package your Python code into an .exe file!

Have you ever thought about how your small programs (like a tool for automatically organizing files or calculating grades) can not only be used by you but also shared with friends and colleagues, allowing them to run it with just a double-click without needing to install Python? This is achieved through “packaging”! Today, I will guide you through the simplest way to turn your Python script into a real “small software”!

🧰 Why package into .exe?

Imagine this: you wrote a super useful “daily check-in reminder” program, and your friend wants to use it. But if they haven’t installed Python and haven’t learned any code, your code is like “a book written in a foreign language” to them.

However, if you can turn it into a .exe file, just like the installation packages for QQ or WeChat, your friend only needs to double-click to run, and everything is done! This is the charm of packaging!

🛠️ What tool to use for packaging? Recommended: PyInstaller

In the world of Python, there are many packaging tools, but the most popular and simplest one is PyInstaller. It acts like a “magic box” that can pack your .py files and all dependencies into it, ultimately outputting a standalone .exe program.

Step 1: Install PyInstaller

Open your command line (Windows users press Win + R, type cmd and hit enter), then type:

pip install pyinstaller

Wait for the installation to complete, and you will have the “superpower” of packaging!

💡 Tip: If it says pip is not a command, it might be because you haven’t installed Python or haven’t added Python to your environment variables. Don’t panic! When reinstalling Python from the official website, remember to check “Add to PATH”.

📦 Packaging the first .exe file

Suppose you have a file named hello.py with the following content:

 hello.py
name = input("Please enter your name:")
print(f"Hello, {name}! Welcome to the world of Python!")
input("Press Enter to exit...")   Prevent the window from flashing by

Now, let’s package it into an exe!

Step 2: Navigate to the directory of the file

In the command line, use the cd command to switch to the folder where hello.py is located. For example, if your file is in D:\my_python, type:

cd D:\my_python

Step 3: Execute the packaging command

Type this command:

pyinstaller -F hello.py

Explanation:

  • pyinstaller is the tool name
  • -F means “package into a single exe file” (Single File)
  • hello.py is the file you want to package

⚠️ Note: The first packaging may take a while (a few minutes) because PyInstaller needs to collect all dependencies. Be patient and wait for it to finish!

After successful packaging, you will see several new folders in your directory: build, dist, and a .spec file.

Your .exe file is in the dist folder! Open it to see, hello.exe is your “finished product”!

🎨 Make the exe look better: Add an icon!

The default exe icon is a small gear, which isn’t very cool, right? We can change it!

Prepare an .ico icon file (for example, named myicon.ico), and place it in the same directory as hello.py .

Then use this command to package:

pyinstaller -F -i myicon.ico hello.py
  • -i parameter is used to specify the icon.

Next time your program will have a personalized icon, making it look more professional when shared with others!

💡 Tip: Don’t have a .ico file? Search “online generate ico icon” on Baidu, upload an image, and it can be converted!

🧩 Handling complex projects: What if there are multiple files or modules?

If your project has more than one .py file, for example:

  • main.py (main program)
  • utils.py (utility functions)

As long as main.py can run normally (for example, if python main.py works), you can directly package main.py !

PyInstaller will automatically analyze which modules and functions you used and package them all in.

For example:

 utils.py
def greet(name):
    return f"Hello, {name}!"

 main.py
import utils
name = input("Name:")
print(utils.greet(name))
input("Press Enter to exit...")

Still use this command to package:

pyinstaller -F main.py

It will automatically package utils.py as well, without you having to worry about it!

⚡ Small optimization: Remove the black window (suitable for GUI programs)

The above example will pop up a black command line window. If you are making a GUI program (like using tkinter), you might not want to see the black box.

For example, if you have a graphical program gui_app.py:

 gui_app.py
import tkinter as tk

root = tk.Tk()
root.title("My Small Tool")
label = tk.Label(root, text="Hello GUI!")
label.pack()
root.mainloop()

When packaging, add the -w parameter to hide the black window:

pyinstaller -F -w gui_app.py

This way, when running the exe, only your graphical interface will appear, clean and refreshing!

❌ Common issues and pitfalls guide

  1. Exe crashes after packaging? It is likely that the program exits immediately after running. Solution: Add input("Press Enter to exit...") at the end of the code or use -w for GUI.

  2. Module not found? Ensure all .py files are in the same directory or correctly use import.

  3. Exe mistakenly deleted by antivirus software? Many antivirus programs mistakenly identify packaged exes as viruses (because they are “packaged”). This is normal; you can temporarily disable the antivirus or upload it to a platform for others to try.

  4. Generated exe too large? An empty program may be 5-10MB after packaging because it includes the entire Python interpreter. Don’t worry, this is normal!

✅ Summary of what we learned today

Today we learned:

  • How to use PyInstaller to convert Python scripts into .exe files
  • Using -F parameter to package into a single file
  • Using -i parameter to add a custom icon
  • Using -w parameter to hide the black window (suitable for graphical programs)
  • How to handle multi-module projects
  • How to troubleshoot issues

This technique is especially suitable for when you want to share a small tool with friends or make your program “look more professional”.

💬 Give it a try!

Now, I have a small task for you:

  1. Write a simple Python program, like a “calculator” or “lottery tool”
  2. Use pyinstaller -F to package it into an exe
  3. Change to an icon you like
  4. Send it to a friend to see if it runs!

If you encounter any issues, don’t be afraid; see you in the comments! I can help you troubleshoot together.

Friends, today’s Python learning journey ends here! Remember to code, and feel free to ask Puff in the comments if you have any questions. Wishing everyone a happy learning experience and continuous improvement in Python!

Leave a Comment