Chaostreff-Potsdam / erika3004

19 stars 5 forks source link

Erika CLI: move specific imports into functions #70

Closed ArchibaldBienetre closed 4 years ago

ArchibaldBienetre commented 4 years ago

On a RasPi, it's not as easy to just install all requirements from requirements.txt as on a "normal" machine.

E.g., the CLI will fail if the Python Imaging Library (pillow) is missing - even though the user may not even need it.

To solve this, move import statements to the specific methods of cli.py

ArchibaldBienetre commented 4 years ago

I tried around a bit - if a module uses an import (like import numpy as np), this would have to be moved into every method that uses it.

This sounds a bit weird... and we may miss a lot of imports here.

Instead, I'll rather work closely with the RasPi crowd and identify which imports to remove, specifically. @cyroxx and @sirexeclp can already give me some concrete pointers, e.g. PIL / Pillow was mentioned (see initial comment)

Maybe this commit here will already to the trick for you? https://github.com/Chaostreff-Potsdam/erika3004/commit/c58bf93c25d868daaffe59a2d5860aeb6833a10f

sirexeclp commented 4 years ago

moving imports to methods makes only sense for the cli module, where multiple subprograms come together that can work independent of each other everything that is always needed can be a global import in the "sub-modules" imports can and in fact should be global The cpp idea: "Pay Only For What You Use" fits here, i guess.

ArchibaldBienetre commented 4 years ago

Got it - I overlooked that the erika.cli module also tries loading a erika.image_converter.WrappedImage to guess if an input image is binary or ASCII art. See erika.cli.get_erika_for_given_args

Confusing: There is no import for PIL nor erika.image_converter.WrappedImage in erika.cli. (though this bit of code absolutely provably works on my machine). I guess I need to challenge my knowledge and assumptions regarding python imports :thinking:

EDIT: It seems the from X import * statement was quite magical.
Moving imports to functions / methods, its usage is not an option (at least the IDE warns me), and I need to do single imports for stuff. Then, imports for erika.image_converter do show up.

sirexeclp commented 4 years ago

right i guess from X import * loads the whole module, which executes all statements i.e. other import statements in this module from X import Y only loads the specified function or class btw thats why people write if __name__ == "__main__" because without it the code would be executed, when the module is imported