App created with PyInstaller has a slow startup

Question:

I have an application written in Python and ‘compiled’ with PyInstaller. It also uses PyQt for the GUI framework.

Running this application has a delay of about 10 seconds before the main window loads and is shown. As far as I can tell, this is not due to slowness in my code. Instead, I suspect this is due to the Python runtime initializing.

The problem is that this application is started with a custom laucncher / taskbar application. The user will click the button to launch the app, see nothing appear to happen, and click elsewhere on another application. When my application shows it’s window, it cannot come to the foreground due to the rules for SetForegroundWindow.

I have access to the source for the PyInstaller win32 loader, the Python code, and even the launcher code.

My questions are:

  • How can I make this application start faster?

  • How can I measure the time spend i the first few seconds of the process’s lifetime?

  • What is the generally accepted technique for reducing time until the first window is shown?

I’d like to avoid adding a splash screen for two reasons – one, I expect it won’t help (the overhead is before Python code runs) and two, I just don’t like splash screens 🙂

If I need to, I could probably edit the PyInstaller loader stub to create a window, but that’s another route i’d rather not take.

Asked By: EB.

||

Answers:

I have ‘compiled’ a few wxPython apps using py2exe and cx_Freeze, None of them take more than 4 seconds to start.

  • Are you sure sure it’s not your code ?
    maybe some network or some I/O resource call holding your app ?
  • Have you tried other machine than yours? Even the fastest hardware can be slow sometimes with the wrong software config, apps or OS, try it.
  • Try timing it with timeit module.

I never used pyQT, but with wxPython the startup speed is OK, and after the first initialize if I close and open again, it’s faster than the first time.

I suspect that you’re using pyinstaller’s “one file” mode — this mode means that it has to unpack all of the libraries to a temporary directory before the app can start. In the case of Qt, these libraries are quite large and take a few seconds to decompress. Try using the “one directory” mode and see if that helps?

Answered By: Shish

Tell PyInstaller to create a console-mode executable. This gives you a working console you can use for debugging.

At the top of your main script, even before the first import is run, add a print “Python Code starting”. Then run your packaged executable from the command line. This way you can get a clear picture whether the time is spent in PyInstaller’s bootloader or in your application.

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. PyQt is generated by SIP which generates very fast lazy bindings; importing the whole PyQt is faster than any other GUI library because it basically does nothing: all bindings to classes/functions are dynamically created when (and if!) you access them, saving lots of memory too.

If your application is slow at coming up, that will be true without PyInstaller as well. In that case, your only solution is either a splash screen (import just PyQt, create QApplication, create a display the splashscreen, then import the rest of your program and run it), or rework your code. I can’t help you much without details.

Answered By: Giovanni Bajo

I agree with above answers. My Qt python program needed about 5 seconds to start up on a decent PC when using onefile mode. After I change to –onedir, it only took around one second to start; almost immediately after user double clicks the exe file. But the drawback is that there are many files in that directory which is not so neat.

Answered By: Russj

For my application, the long startup time almost entirely was caused by the antivirus system. Switching it off reduced the startup in my case from 3 minutes to less than 10secs!

To bring these measurements into perspective: My application was bundled with extra data files (about 150 files with a payload of 250MB), besides carrying around Qt and numpy (that may depend on Intel MKL, which alone adds another 200MB to the bundle!) dependencies. It even didn’t help much that the tested system was running with a solid state drive…

In conclusion: if you have a large application with lots of dependencies, the startup time may be affected strongly by the antivirus system!

Answered By: normanius

In case anyone is still have this issue, I resolved mine by running the exe locally and not on any sharedrives. This took the startup time from over a minute to under 10 seconds.

Answered By: user11886589

I solved this by adding an exception to the anti-virus monitoring software (F-Secure). It removed the wait of several minutes before running.

Answered By: Termintenator