Unable to find module after import

2361 views python
0

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.

answered question

Your structure is wrong. For example to import Gparser, you should write from g_plotter import Gparser as it is in a module.

@Sraw, then in the flask app when running it directly I get "ImportError: cannot import name 'Gparser' from 'g_plotter'". And in my main app, it also errors out with "ModuleNotFoundError: No module named 'Gparser'"

1 Answer

1

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

import g_plotter.Gparser
from . import Gparser
from g_plotter import Gparser

posted this

Have an answer?

JD

Please login first before posting an answer.