My package looks like this:
These helpers, since they are all dealing with scipy
, all have common imports:
from matplotlib import pyplot as plt
import numpy as np
I'm wondering if it is possible to extract them out, and put it somewhere else, so I can reduce the duplicate code within each module?
You can create a file called my_imports.py
which does all your imports and makes them available as *
via the __all__
variable (note that the module names are declared as strings):
File my_imports.py
:
import os, shutil
__all__ = ['os', 'shutil']
File your_other_file.py
:
from my_imports import *
print(os.curdir)
Although you might want to be explicit in your other files:
File your_other_file.py
:
from my_imports import os # or whichever you actually need.
print(os.curdir)
Still, this saves you having to specify the various sources each time — and can be done with a one-liner.
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