5

Apologies for the extremely vague question, but I've been wrecking my head trying to reduce the size of an Pyinstaller .exe and I decided to check what more experienced Python developers think about the general file size of such files. I'm working alone so I really don't know who else to ask.

My script needs quite a lot of modules, including APscheduler (including SQLAlchemy), sqlite3, tkinter, datetime, patool, shutil, but ultimately it's a relatively simple programme that should run as a Windows background service (ultimate goal). No pandas, numpy, scipy, keras or other big libraries are used. I also include 5 .png images for the GUI (around 350 kB total). This creates a 15.5 MB .exe and when running, the programme takes up 30 MB of the physical memory.

All advice online talks about using Python (not Anaconda) to create separate virtual environment for the file(I'm working on doing that now but it has been painful..). Looking at the dist folder Pyinstaller creates, most of the large .dll seem to be essential for the running of the programme (mfc140u.dll, python37.dll, tcl, tk, multiprocessing, sqlite, ..) and there doesn't seem to be pandas or tensorflow or other libraries that are otherwise installed in my conda site packages. I should also mention this is my first running programme, so I imagine the code architecture is not as efficient as it should be.

Particularly APScheduler has quite a lot of dependencies, so I am not sure isolating it to a single virtual environment would have such a huge effect on the size. However, I was told the file should be a lot smaller so it does not burden the OS - a similar but slightly simpler software created in C# is just 64 KB.

Would experienced developers say that this file size is normal given all the libraries/dependencies, and what else can be done apart from creating a separate venv in pure Python?

EDIT: After 3 days and battling a few thousand errors, I managed to make the script run in system Python (as opposed to Anaconda) with its separate virtual environment, in order to install only the packages I need. The result: the file is only about 2 MB smaller. So definitely was not worth it. But for other people it might be I guess.

ISquared
  • 364
  • 4
  • 22

2 Answers2

3

15 megabytes of disk and 30 megabytes of memory isn't really much in today's terms at all. (IMHO, whoever told you it would "burden the OS" is living somewhere in Windows 95 land, or earlier still.)

That file size is normal considering the Python interpreter, standard library, additional libraries and all are bundled in; a C# program being 64 kilobytes and doing the same is benefiting from the C# libraries and virtual machine having been shipped with Windows.

For contrast, in JavaScript land, where desktop apps tend to be Electron based, you start from around a hundred megabytes of disk use...

AKX
  • 152,115
  • 15
  • 115
  • 172
2

The file size is normal. I don't think you can do much more with it.

Pyinstaller bundles python interpreter and supporting files along with it and the win32 dlls which will most probably take up atleast 12MB. You can check this by making a virtualenv with a hello world script and turn it into a exe.

As AKX says it doesn't burden the system much more than running python itself. The large file is not loaded into memory completely. It is unzipped into the %temp% folder with a folder name containing _MEIPASS and only required files are loaded into memory on the target machine. However, there is an overhead as startup to create a folder, unzip the files and delete them after exit.

Also don't compare it to C,C# etc because those languages don't need to bundle additional files like python because they would already be included in your system. Also I haven't used it myself but you may try py2exe. It is old but many apps use it and could reduce the file size a bit.

Canute S
  • 334
  • 1
  • 5
  • 12