When a package becomes large, it can be hard to remember where things are, and dot-paths to the object we want can be cumbersome. One way that authors seem to address this is to bring references to the "best of" objects to the top, though there code can actually live several package levels below.
This allows one to say:
from pandas import wide_to_long
instead of
from pandas.core.reshape.melt import wide_to_long
But what are the ins and outs of doing this, and the best practices around the method? Doesn't loading the top __init__.py
with many imports (in order to make them available at the top level) mean that any import of a single object of the package suddenly takes much more memory than needed - since everything mentioned in the __init__.py
is automatically loaded?
Yet, packages do it. See, for example, what can be imported from top level numpy
or pandas
below (code to run your own diagnosis can be found in this gist).
$ python print_top_level_diagnosis.py numpy
--------- numpy ---------
599 objects can be imported from top level numpy:
19 modules
300 functions
104 types
depth count
0 162
1 406
2 2
3 29
4 1
$ python print_top_level_diagnosis.py pandas
--------- pandas ---------
115 objects can be imported from top level pandas:
12 modules
55 functions
40 types
depth count
0 12
3 37
4 65
5 1