How to convert .py to .exe? Step by step guide.

Proxlight
4 min readDec 9, 2020

Auto PY to EXE

The only tool that we are gonna be using is Auto PY to EXE!

Created By Pratyush Mishra

Auto PY to EXE is an amazing application for making .exe files out of your project whether it is one .py file or any number of them.
The application has a nice GUI and looks like this:

How to start

Video Tutorial

Step 1. Installation

Installing using PyPI:

To install the application run this line in cmd:

pip install auto-py-to-exe

To open the application run this line in cmd:

auto-py-to-exe

Note: if you have problem installing this way or you want to install it from GitHub go to the main page or watch this instructional video by the developer of “Auto PY to EXE” himself.

For more additional information use this

“Issues When Using auto-py-to-exe”

Step 2. Converting

There are few main options you need to choose:

  1. Pick your .py file
  2. Pick “One Directory” or “One File” option
  3. Pick additional files

1. Pick your .py file

If you have multiple files choose one that starts the program.

2.1. “One Directory” option

Pretty simple. When choosing “One Directory” option “Auto PY to EXE” will put all dependencies in one folder. You can choose Output directory in “Advanced” menu. If you have media files like icons and backgrounds you should’t have any problems using them inside your .exe if you place media files/folders in Output directory.
Something like this:

2.2. “One File” option

When choosing “One File” option “Auto PY to EXE” will create one .exe file containing all dependencies but NOT MEDIA FILES. If your program has only default Windows gui with no icons, backgrounds, media files or you are OK with placing media folder with .exe file feel free to skip the following explanation. For those who want to pack media files into .exe file itself read paragraph 3.

3. Pick additional files

There is a menu in “Auto PY to EXE” called “Additional Files” that lets you add files of your choice. There is a catch though. “Auto PY to EXE” uses pyinstaller which unpacks the data into a temporary folder, and stores this directory path in the _MEIPASS environment variable. Your project won’t find necessary files because the path changed and it won’t see the new path eather. In other words, if option “One File” is choosen picked files in the “Additional Files” menu will not be added to .exe file. To work around this you should use this code provided by developer of Auto PY to EXE here

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)

To use this code in your project replace the link to the media file you have now
For example:

setWindowIcon(QIcon('media\icons\logo.png'))

with

setWindowIcon(QIcon(resource_path('logo.png'))

Now the link will be referenced correctly and chosen files successfully packed into .exe file.

For comparison:
Possible link before

"C:\Users\User\PycharmProjects\media\icons\logo.png"

Possible link after

"C:\Users\User\AppData\Local\Temp\\_MEI34121\logo.png"

Press CONVERT .PY TO .EXE

Wait

Step 3. Run your program!

Now everything is done!

Run it. Test it. See what`s up.

Make sure everything works well.

You made One Directory

Every file you need should be in a single directory.

You made One File

This way you should have single .exe file. If you had a need and if done correctly your .exe file will be packed with all media inside it. You will not need any media files/folders present with .exe file for it to display them properly.

If you have any feedback or suggestions on what important information should be added feel free to let me know!
This guide is not a description of every possible option done every possible way.
I hope you found that information useful!
Good luck with your projects!

--

--

Proxlight

Welcome to Proxlight, a place for all to learn coding and fun stuff.