nialloc
nialloc

Reputation: 852

Python cannot import a class because it's base module cannot be found

I have a folder

/app
  __init__.py
  main.py
    /folder
        __init__.py
        module1.py
        module_base.py

In main.py I try to import a class from module1.py

from folder.module1 import ModuleThing

PROBLEM: I get an error when I try to run main.py saying ModuleNotFoundError: No module named module_base

module1.py looks like this

from module_base import ModuleBase

class ModuleThing(ModuleBase):
    """
    """< and so on >

module_base.py looks like

class ModuleBase:
    """
    """< and so on >

I can't remember the correct arrangement of init.py files and so on to get this right. Any help is very much appreciated.

Upvotes: 0

Views: 308

Answers (1)

countunique
countunique

Reputation: 4296

Imports are absolute by default. So you could use the absolute form:

from folder.module_base import ModuleBase

Or you could use the relative form:

from .module_base import ModuleBase

The Python tutorial on Modules is a good resource to learn more.

Upvotes: 1

Related Questions