I have a python script that is becoming rather long. Therefore, the functions defined in the rather large single script were written into individual files for easier maintenance and to easily share them between different main scripts.
In the single script, I import numpy and other modules at the top of the file. Now, if the function is written into a separate file, I need to import numpy in that separate file. I'd rather avoid that because with multiple functions it will end up importing numpy several times.
Can this be done? Thanks
Yes it can be done, as described here: Python: Importing an "import file"
In short, you can put all imports in another file and just import that file when you need it.
Note though that each file needs to have numpy imported, one way or another.
EDIT:
Also read this: Does python optimize modules when they are imported multiple times? to understand how python handles multiple imports. Thanks to @EdChum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With