Search code examples
pythonpyinstaller

Pyinstaller, only packing part of the employed third-party packages, and calling the remaining on another PC


  1. On PC1, I have a program in which package A B C is employed, and I'd like to pack the program by Pyinstaller together with package A B (excluding C for some reason), obtaining "main.exe".
  2. Assuming that there's another machine PC2 installed with python and package C, I hope to run this "main.exe" (without package C embedded) on PC2, during which package C installed on PC2 is called.

Is this realizable? and How?

I pack the program on PC1 by:pyinstaller main.py --exclude-module C and as expected, failing to execute main.exe on PC2 with ModuleNotFoundError: No module named 'C', since package C installed on PC2 can not be recognized by main.exe.

To be precise, Package C mentioned refers to "geatpy" http://geatpy.com, Package A, B refer to Numpy, Pandas, xlwt, etc. Bundling all of these into one bundle and running it, some errors occur:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    from optimization import *  
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "optimization.py", line 20, in <module>
    import geatpy as ea  
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "geatpy\__init__.py", line 16, in <module>
ModuleNotFoundError: No module named 'awGA'
[48140] Failed to execute script main

After enquiring with the developer of this module, he suggests excluding this module in the bundle, and install via pip cmd instead, because pyinstaller may be not well compatible with pyd/dll file. I am quite new to coding, so maybe I can't get his point or there may be some misunderstanding.


Solution

  • Finally, I found a solution by manually copy all the .pyd file in folder Python38\Lib\site-packages\geatpy into the pyinstaller-produced folder dist\main, and I do it the same way for several other missed .py modules warned during execution.

    To generate the dist\main folder, pyinstaller -c main.py is applied instead of bundling it into one executable file.

    As buran say, the missing import problem is due to the inability of pyinstaller to detect what is needed, especially for .pyd file.

    I try some other arguments for pyinstaller but fails, like: --paths=.... --hidden-import=geatpy. Maybe that's the limitation of pyinstaller.

    For the second suggestion of buran, I haven't tried it yet, but it's believed to be feasible.