Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How can I use __init__.py effectively?

I have a library, which I'll simplify/anonymize to the following: The directory structure is something like this:

lib/
    __init__.py
    foo.py
    bar.py
    baz.py
    quux.py

Most of the code is implementation detail. I want to have a simple way for users to import and use the public API. I want them to be able to simply do from lib import something. To that end, I put the following in __init__.py:

from . import foo
from . import bar

something = foo.something
A_Class = bar.A_Class

However, when I import lib, I don't see any of those names. When I run from lib import something I get an ImportError complaining of an unknown location, and when I:

import lib
lib.something

I get an AttributeError complaining that lib has no attribute something.

What am I doing wrong? By the way, the lib only needs to run in Python >= 3.6.

Comments