Help with converting .py file into .exe


I created a game using python a while back, but I want to turn it into an executable so that I can easily share it with friends and family. I’ve spent hours trying to do this, but none of the articles or tutorials I use seem to work. The most recent method I used created the executable file, but when I tried to open it nothing happened. I have tried doing it on more than one device, so I don’t think it’s a problem with my computer.
Any help is much appreciated!


Ibrahim Piri

Try using pyinstaller library

Yes, I already tried that. It didn’t work.

I’ll second @adramelech here and say pyinstaller. However, it can be troublesome when building from larger projects that utilize a lot of libraries outside the python standard library or non-python files (like images, config files, and data files). To pyinstaller, there are often “hidden dependencies” in many python libraries that don’t get automatically pulled in when you run it. I have this issue whenever I use pandas for instance. No errors generated until I run the executable (if it runs at all). To get around that there is a config file that you can have pyinstaller generate and than edit it to pull in all the things it needs to make an executable. You will likely need to dive into their more advanced documentation sections for this.

I would consider trying auto-py-to-exe first. It uses pyinstaller under the hood but walks you through the process with a GUI and I find it makes tweaking some of pyinstaller’s more advanced settings and adding in dependency files easier.

Also, I would suggest making sure any anti-virus software you may have running isn’t interfering with the executable or pyinstaller.

Alternatively, while I know it’s not nearly as convenient as an executable and isn’t always appropriate for a lot of use cases, have you thought about using a bash/batch script?

auto-py-to-exe appears to be giving me my most promising results so far, but I am encountering problems with my imports, namely pygame. It appears to be the same issue that @kylec mentioned, but I am struggling to fix it. I tried using the --hidden-import setting in auto-py-to-exe, but I think I might be using it wrong because I am still getting the same error.

I’m not sure I completely understood @kylec’s solution. Can you please elaborate more on how I could get around this problem?

Without knowing what dependencies pyinstaller is struggling with (if that is the issue at all), I can’t really tell you how to fix it.
On googling “pygame pyinstaller” it looks like people tend to run into two types of issues, using pygame fonts that aren’t automatically imported or missing data files (--add-data).
The best advice I can give is to set pyinstaller’s --debug=all flag and follow any tracebacks that arise. You may also want to take a look at PyInstaller’s advice for the situation

I am using a font that I found online and I made sure to include it in the --add-data section, so I don’t think the issue is there. Do I need to import the pygame font module separately?

I am sure I have included all the data files I explicitly used in my code, are there other files I need to add for pygame to work?

I tried using --debug=all; the traceback is giving me an error on the first line of code, which is where I import pygame, which once again makes me think that the problem isn’t with fonts or my files. Aside from that, the tracebacks weren’t very helpful, but I included them below anyway. I can also include my code if that would be convenient.


*The name of my game is Defend Anadeia, and is the file that I am trying to convert into a .exe file.

Is pysinstaller running from the same environment as the game development environment? Like if you’re using venv or conda to manage different python environments, is pyinstaller installed in the same env as the game?

If they are running in the same environment, is pygame imported in any secondary scripts that are being imported into your main script? You can try putting pygame and whatever other modules it’s not finding in the --hidden-imports command. Also using the --debug=imports flag may help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.