pythonpython-3.ximportpython-import

Using a seperate python function to import dependencies


Simple question really: I am build a standard repo for doing few simple machine learning tasks, and I almost always import the same dependencies in those .py modules.

I was thinking that it would un-clutter my code a bit (and save time) if I could make a .py file that imported all my standard dependencies. This could then be expanded to different classes that depend on the model e.g. sklearn, xgboost, etc.

1) Is is advisable practise to call dependencies from a external function? 2) If so, how would this be done best?

So far, I've tried the following:

project_dependencies.py contains

def get_dependencies():
    import numpy as np

if __name__ == "__main__":
    get_dependencies() 

and model.py might then contain

from project_dependencies import get_dependencies

get_dependencies()

def model():
    return np.random.normal(1)

if __name__ == "__main__":
    model()

But that does not seem to work. What would a good, long term solution to this be? Thanks!


Solution

  • The general advice is to import the required modules in the module(s)/file(s) where you need them. That occasionally requires some extra typing, but makes each file/module more stand-alone.

    An alternative (but less recommended) way could be to import all the dependencies in your package's __init__.py file (and hardly anything else), then do a relative import in your other package modules, like from . import *.
    Though often, __init__.py tends to be used the other way around: it (relative) imports everything defined by __all__ in the package modules.


    Basically, look at some other well-known bigger projects, and see what they do. Django, requests, NumPy etc. You may find that they, overwhemlingly, import external dependencies inside each module separately, and use relative imports just for internal dependencies (classes, functions etc). That is, they use the first option.