How to prevent PyInstaller packaging unused modules

35 views
Skip to first unread message

Brendan Simon (eTRIX)

unread,
Nov 7, 2019, 4:27:37 AM11/7/19
to pyins...@googlegroups.com
I have an app and am experimenting with various modules (opencv, skimage, harvester etc).

Using PyInstaller my self contained exe (--onefile) is huge (120+MB) and takes a long time to startup.

I recently modified the code selectively import various modules if they are required (to see which ones perform best and how they get packaged), but PyInstaller seems to import/collect/package them even if the app never uses them.

simple example.

CONFIG_USE_OPENCV = True
CONFIG_USE_SKIMAGE = False
CONFIG_USE_HARVESTER = False

if CONFIG_USE_OPENCV :
    import cv2 as cv

if CONFIG_USE_SKIMAGE :
    from skimage.transform import rescale

if CONFIG_USE_HARVESTER :
    from harvester import Harvester

Am I right that PyInstaller will include all modules (opencv, skimage and harvester) in the bundle regardless of the CONFIG settings ?

Is there a way to get PyInstaller to only package the modules that the app will use (other than commenting out code everywhere?)

Thanks, Brendan.

Hartmut Goebel

unread,
Nov 7, 2019, 5:44:38 AM11/7/19
to pyins...@googlegroups.com
Am 06.11.19 um 23:35 schrieb Brendan Simon (eTRIX):
Is there a way to get PyInstaller to only package the modules that the app will use (other than commenting out code everywhere?)

pyinstaller --help is your friend.

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP, ISO 27001 Lead Implementer
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Blog: https://www.goe-con.de/blog/kamera-safari-durch-die-munchner-innenstadt
Kolumne: https://www.goe-con.de/hartmut-goebel/cissp-gefluester/2011-02-fleisige-datensammler-fur-lukratives-geschaeftsmodell-gesucht

0x7B752811BF773B65.asc

Brendan Simon (eTRIX)

unread,
Nov 7, 2019, 7:11:22 AM11/7/19
to pyins...@googlegroups.com

On 7/11/2019 9:44 pm, Hartmut Goebel wrote:

Am 06.11.19 um 23:35 schrieb Brendan Simon (eTRIX):
Is there a way to get PyInstaller to only package the modules that the app will use (other than commenting out code everywhere?)

pyinstaller --help is your friend.

I have been through all the documents and stackoverflow.  I am using a spec file and have been playing with filtering out various files before producing the exe.  e.g. removing all the ".npy" and ".npz" data files that I don't need.

So running `pyinstaller --help` did help me focus back on `--exclude-module`.  This by itself doesn't do what I want, which is for pyinstaller to not include modules it never sees (or what the app would never see if run).  Maybe that's not possible to do?

Anyway using `--exclude-module` at command line would mean I need different scripts with different sets of command arguments (or one script with some settings).

Thinking about it more, I could probably do it in the spec file too, by importing those CONFIG attributes from the app and setting the `excludes` list as required.

I'll give that a whirl.

Vielen Dank !


Jones, Bryan

unread,
Nov 7, 2019, 10:49:04 AM11/7/19
to pyins...@googlegroups.com
Another approach: create a venv and install only what you want packaged.

The Pyinstaller dependency analysis is conservative -- if there's a case where package x might be needed, it will be included. It can't tell what your use-case is.

--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/6fc2db98-9829-19bb-92cb-3173c0537286%40etrix.com.au.


--
Bryan A. Jones, Ph.D.
Associate Professor
Department of Electrical and Computer Engineering
231 Simrall / PO Box 9571
Mississippi State University
Mississippi State, MS 39762
http://www.ece.msstate.edu/~bjones
bjones AT ece DOT msstate DOT edu
voice 662-325-3149
fax 662-325-2298

Our Master, Jesus Christ, is on his way. He'll show up right on
time, his arrival guaranteed by the Blessed and Undisputed Ruler,
High King, High God.
- 1 Tim. 6:14b-15 (The Message)

Hartmut Goebel

unread,
Nov 7, 2019, 11:13:34 AM11/7/19
to pyins...@googlegroups.com
Am 07.11.19 um 12:54 schrieb Brendan Simon (eTRIX):
This by itself doesn't do what I want, which is for pyinstaller to not include modules it never sees (or what the app would never see if run).  Maybe that's not possible to do?

???

PyInstaller eo exactly this: Include only the modules it "sees". Which is: are imported by your code (or recursively by any module your code uses) and are avalably in you Python installation.

e.g. removing all the ".npy" and ".npz" data files that I don't need.

I never hear about such files. What is it?

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP, ISO 27001 Lead Implementer
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

0x7B752811BF773B65.asc

Brendan Simon (gmail)

unread,
Nov 8, 2019, 3:45:28 AM11/8/19
to pyins...@googlegroups.com

On 8/11/2019 3:13 am, Hartmut Goebel wrote:

e.g. removing all the ".npy" and ".npz" data files that I don't need.
I never hear about such files. What is it?

Numpy arrays stored to file.  ".npy" files have a header followed by binary data.  ".npz" are compressed versions of ".npy".  opencv and/or skimage have a bunch of data files (sample images), which the user can load for testing algorithms, etc.

PyInstaller adds them to the bundle, even if I never use any of them.  To save space removing these from the "datas" list in my spec file.  Maybe there is a better way by using hooks, but it's all just experimental at the moment.

Brendan.


Reply all
Reply to author
Forward
0 new messages