Q:

Unable to find module after import

I'm runnin Python 3.6.3 and I have the following module in a subdirectory that I'm trying to install through pip.
/g_plotter
          setup.py
          /g_plotter
                    __init__.py
                    g_plotter.py
                    Gparser.py

setup.py
from setuptools import setup

setup(
    name='g_plotter',
    packages=['g_plotter'],
    include_package_data=True,
    install_requires=[
        'flask',
    ],
)

I installed that module form Docker in my container:
RUN pip3 install ./g_plotter

Then in my app code:
import g_plotter

print(dir(g_plotter))

which outputs
 server_1  | ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

so using this import instead:
from  g_plotter import g_plotter

results in
server_1  | Traceback (most recent call last):
server_1  |   File "./g_server.py", line 21, in <module>
server_1  |     from  g_plotter import g_plotter
server_1  |   File "/usr/local/lib/python3.7/site-packages/g_plotter/g_plotter.py", line 7, in <module>
server_1  |     import Gparser
server_1  | ModuleNotFoundError: No module named 'Gparser'

When I run the submodule on it's own (It's a flask app), it works.

A:

You must use absolute import in python 3, import Gparser is not allowed any more. You can change it to:
from . import Gparser
from g_plotter import Gparser

Let you more clear, I will describe what do they mean.
import Gparser
Gparser = load_module()
sys.modules['Gparser'] = Gparser

from g_plotter import Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser

from . import Gparser
package = find_package(__name__)  # 'g_plotter'
Gparser_name = package + 'Gparser'  # g_plotter.Gparser
Gparser = load_module()
sys.modules[Gparser_name] = Gparser

Now you can understand, if you run g_plotter directly, actually __name__ is __main__, so python can't find a package from it. Only if you import this submodule in other module, from . import something can be worked.

