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

eshleron

Eshleron

Posted on January 24, 2019

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

Auto PY to EXE

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

Auto PY to EXE is an amazing application for making .exe file 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:

alt text

How to start

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 any problems 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

alt text

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 shouldn't have any problems using them inside your .exe if you place media files/folders in the Output directory.
Something like this:

alt text

2.2. "One File" option

alt text

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 near .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 either. In other words, if option "One File" is chosen 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)
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

with

setWindowIcon(QIcon(resource_path('logo.png'))
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Possible link after

"C:\Users\User\AppData\Local\Temp\\_MEI34121\logo.png"
Enter fullscreen mode Exit fullscreen mode

Press CONVERT .PY TO .EXE

alt text

Wait

alt text

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 the 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.


P.S.

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!

馃挅 馃挭 馃檯 馃毄
eshleron
Eshleron

Posted on January 24, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related