I have this structure:
app.py
sub/
__init__.py
lib.py
utils.py
In app.py
, I want to be able to do sth like this:
from sub.lib import some_func
In lib.py
, I want to be able to import utils.py
.
And I also want that it's possible to execute lib.py
as a script directly. (I.e. cd sub; python3 lib.py
.)
The problem is, if I start lib.py
as a script, it will not be a package, and thus, it cannot use relative imports, so sth like from .utils import some_util_func
would not work. However, if I import lib.py
as part of the sub
package, only the relative import will work.
How do I solve this? (In an easy way. E.g. without creating another small wrapper script just to call lib.py
.)
E.g. if there is a way to mark the __main__
module as a package, it would solve that. But how? Is this possible? E.g. by defining __path__
or so?