Importing a function/class from a Python module of the same name
- by Brendan
I have a Python package mymodule with a sub-package utils (i.e. a subdirectory which contains modules each with a function). The functions have the same name as the file/module in which they live.
I would like to be able to access the functions as follows,
from mymodule.utils import a_function
Strangely however, sometimes I can import functions using the above notation, however other times I cannot. I have not been able to work out why though (recently, for example, I renamed a function and the file it was in and reflected this rename in the utils.__init__.py file but it no longer imported as a functions (rather as a module) in one of my scripts.
The utils.__init__.py reads something like,
__all__ = ['a_function', 'b_function' ...]
from a_function import a_function
from b_function import b_function
...
mymodule.__init__.py has no reference to utils
Ideas?