Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyInstaller loads script multiple times

I've managed to get pyinstaller to run more or less correctly now, except that it opens too many windows. It's pygame project, and it keeps loading the entire thing over again every second or so. My PC fills with game windows after a few seconds and everything grinds to a halt.

Running it from commandline, I can just see print outs saying the app is starting pasted over and over again in commandline window. As far as I can tell the Apps aren't closing or exiting, just spawning more and more.

The command I call to start pyinstaller is this:

pyinstaller --onedir main_local.py

The main file looks like this:

# Library imports
import pygame

# Project imports
from multiroidal_client    import MultiroidalClient
from settings              import *
import time

# Main program function
def main():

    # Initialise game class
    game = MultiroidalClient(SCREEN_SIZE, ('127.0.0.1', 8080))

    # Start game loop
    game.main_game_loop()


# Execute main function
if __name__ == '__main__':
    main()

I tried commenting out the if __ name __ ... bit to see if for some reason it was executing the main function and calling a duplicate each time by accident or something. When commented out the exec does nothing, as you might expect.

Any ideas? I've not included any more code because there is loads of it, and I'm not sure which parts are relevant. Let me know if you need to see anything else.

Cheers

EDIT: I added a quick print after the game.main_game_loop() just to check if the script jumped out of the infinite game loop. No such luck. It's loading a parallel instances of the scripts, all running simultaneously.

EDIT: I looked through the pyinstaller docs and managed to get some more debug out. Not sure if it's relevant or not, but here it is. This screen of dialog just keeps reprinting over and over again. I also tried the --noupx option which can sometimes help apparently - no such luck.

enter image description here

like image 499
Oliver Avatar asked Sep 19 '15 19:09

Oliver


People also ask

Does PyInstaller need UPX?

PyInstaller looks for UPX on the execution path or the path specified with the --upx-dir option. If UPX exists, PyInstaller applies it to the final executable, unless the --noupx option was given. UPX has been used with PyInstaller output often, usually with no problems.

Why is PyInstaller slow?

PyInstaller's bootloader is usually quite fast in one-dir mode, but it can be much slower in one-file mode, because it depacks everything into a temporary directory. On Windows, I/O is very slow, and then you have antiviruses that will want to double check all those DLL files. PyQt itself is a non-issue.

What is Onefile in PyInstaller?

Under macOS, PyInstaller always builds a UNIX executable in dist . If you specify --onedir , the output is a folder named myscript containing supporting files and an executable named myscript . If you specify --onefile , the output is a single UNIX executable named myscript .

Does PyInstaller install dependencies?

pip will install PyInstaller's dependencies along with a new command: pyinstaller . PyInstaller can be imported in your Python code and used as a library, but you'll likely only use it as a CLI tool. You'll use the library interface if you create your own hook files.


1 Answers

Multiprocessing!

I tried some simpler python programs, and they packaged up all fine. So it wasn't a problem with my method it was the code. Thinking about it again, it must be some unusual or difficult to package code in the project. Hmmm, maybe threading?

I've got a couple of threads running in parallel, and they are multiprocessing threads. After googling about I found this magic fix.

You simply stick this line, multiprocessing.freeze_support(), straight after if __name__ == "__main__" and before you call the main() function.

if __name__ == '__main__':

    # Pyinstaller fix
    multiprocessing.freeze_support()

    main()

It looks like the guys who develop the multiprocessing module had to include a hack to allow freeze packaging (py2exe, pyinstaller etc). Seems weird it isn't included a bit more smoothly. If you leave that freeze_support() call in, it still works even when you aren't running from packaged code as opposed to standard python files.

Anyway, if you are freezing/pacakaging code up and it seems to execute itself over and over, check if you are use multiprocessing. A simple google "multiprocessing pyinstaller" gave the answer.

FYI this only works on --onedir mode, --onefile mode is a different story and not supported on windows. You'll have to dig around more to solve that one.

like image 59
Oliver Avatar answered Sep 19 '22 14:09

Oliver