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:
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:
There are few main options you need to choose:
- Pick your .py file
- Pick "One Directory" or "One File" option
- Pick additional files
If you have multiple files choose one that starts the program.
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:
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.
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 Max in stackoverflow question
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
Now the link will be referenced correctly and chosen files successfully packed into .exe file.
Possible link before
Possible link after
Press CONVERT .PY TO .EXE
Now everything is done!
Run it. Test it. See what`s up.
Make sure everything works well.
Every file you need should be in the single directory.
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!