Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit import from submodules

Tags:

python

module

I've got a package like this:

 foo/
    __init__.py
    bar.py
    baz.py

I'd like to have submodules' functions automatically available when I import foo. So if bar.py has def spam() somewhere, I'd like to be able to call it directly like foo.spam(). What would be the best way to achieve that?

like image 784
georg Avatar asked May 21 '26 06:05

georg


1 Answers

Inside __init__.py, do from bar import spam.

like image 194
Daniel Roseman Avatar answered May 23 '26 19:05

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!