'How to run a module that imports from a sibling package as a script?

With the following structure:

+--demo
|  +-- __init__.py
|  +-- app.py
|  +-- api
|      +-- __init__.py
|      +-- api.py
|  +-- customers
|      +-- __init__.py
|      +-- contacts.py

In the api.py:

from demo.customers import contacts

When I run this, there's no error message.

python3 app.py

But when I tried to run the api.py by itself, I got an error message saying no module named "demo".



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source