9

After 3 days, I can't get a python program packaged into a .exe file. I've tried py2exe (which continuously missed modules), and PyInstaller.

Here's the complicated part. My program uses a lot of additional installed modules (coopr, pyomo, openpyxl, glpk, cbc, pyutilib, numpy, etc.). These in turn import all kinds of other things, and I can't track it down (the PyInstaller warning log lists 676 lines of missing or potentially unneeded modules.)

However, I've gotten (by adding imports of "missing" modules to my program) a .exe version which runs from double clicking or from the command line, without printing any error.

The problem is, the program does nothing. I have an input file which is included in the build, which my program reads in, does some (intense) calculations, and then creates a .csv output file in the same directory. It works as a .py file. My .exe does nothing.

So, if you can tell me what's wrong go ahead. If not, I'd like to know any helpful steps or ideas to try. At this point, I've exhausted the feedback I can find from the program and documentation.

Dylan Cross
  • 544
  • 2
  • 6
  • 14
  • 4
    I'm tempted to vote to close this question as too broad as you post no code. However, I too have been bitten by this same problem. I have something that PyInstaller compiled with no warnings that does nothing when I try to run it. I had no idea how to begin to debug the problem. So I guess that's the essence of your problem. How do you debug a compiled program that appears to do nothing (no error messages and no desired behavior either.) – Steven Rumbalski Sep 15 '15 at 18:24
  • @StevenRumbalski Thanks for the sympathy. – Dylan Cross Sep 15 '15 at 18:29
  • I'm having the same issue. It further bothers me that running from console prints nothing. – Mefitico Oct 01 '19 at 17:49

5 Answers5

10

I just solved this problem for myself.

Make sure you do not have a folder with the same name as the script you are trying to turn into an executable.

If you already have application file sample.py as well as folder sample (that contains your other .py files, say) and you want the application to retain the name sample, you can work around this problem by renaming sample.py to sample_app.py and then invoke pyinstaller with the --name option e.g., pyinstaller --onefile --name sample sample_app.py The binary created by pyinstaller will be called sample.

Community
  • 1
  • 1
Techniquab
  • 843
  • 7
  • 22
  • This worked for me, I believe that it was because the folder with the same name as my script has the __init__.py file which makes it a module and pyinstaller was trying to build that, anyway you are a life saver thanks for that – sjjk001 Jul 20 '21 at 11:42
7

When creating the exe, make sure that the python script contains

if __name__ == '__main__':
    main()

at the bottom. Otherwise, the python exe will run but since it has nothing to run it will just end.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Kevin
  • 71
  • 1
  • 2
1

I also could not make work pyinstaller properly, but a few years ago I found a solution to generate R scripts executables on windows, so I tried it with python and it worked!

Here is the solution, just in 3 lines in the CMD:

https://github.com/jorgeavilacartes/making-my-own-executable-python

You can create your own extension and assing it to the python.exe you wish (if you have many venv you can create one executable extension for each one)

Unfortunately, I only have the solution for windows.

Hope it helps!

PD: Be careful to do not have any .ipynb in the same folder where you will create your executable, it will not run properly, I do not know why, but it was the only problem I had.

Jorge Avila
  • 151
  • 4
1

When an exe fails to run when double-clicked in windows, the associated window also automatically closes. If this happens for you, open the command prompt and try to run the exe there by going to the appropriate path and typing in the filename (e.g. C:\github\program\dist\main.py). Any errors thrown by the application will be printed in the command prompt window, without it automatically closing.

0

If your script accesses non-Python files, move the executable so it can find them.

I was having the same problem. For me, the cause was that some of my Python files required access to non-Python files (in my case, gifs). PyInstaller didn't bundle up the resources along with the Python files and didn't stick the executable in the same directory as the main Python file, so I was getting an error when my program tried to access them. The solution was simply to copy the resources to the location where the executable was looking for them, or vise-versa.

For some reason, the error messages that helped me find the problem didn't generate when I ran PyInstaller normally. They were only generated when using the --onefile flag, and even then, they only stuck around for less than half of a second before the prompt closed. I had to use ctrl+prt to capture my screen when the messages appeared.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Matthew Milone
  • 184
  • 1
  • 9