9

I've scoured through and found lots of questions with lots of answers but nothing seems to be hitting the mark.

I've set up two files config.py and test.py under one folder called test.

config includes the code:

class Config:
    def __init__(self, name):
        self.name = name

while test has:

try:
    # Trying to find module in the parent package
    from . import config
    print(config.debug)
    del config
except ImportError:
    print('Relative import failed')

try:
    # Trying to find module on sys.path
    import config
    print(config.debug)
except ModuleNotFoundError:
    print('Absolute import failed')

This has been put together as per the answer supplier on this stack answer.

Unfortunately I'm getting both errors appearing, when I just try to directly call it from config import Config I get ModuleNotFoundError

I'm truly lost on this one and can't figure out where to go from here.

Using Python 3.6, atom.io as my IDE.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Jake Bourne
  • 723
  • 3
  • 10
  • 27
  • 2
    Is your test folder contain an `__init__.py` file? – Mazdak Apr 30 '18 at 12:51
  • no, I was under the belief that python 3.3+ didn't need it anymore? – Jake Bourne Apr 30 '18 at 12:54
  • 2
    If you want to treat your folder as a package its needed. As is mentioned in documentation: The `__init__.py` files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, `__init__.py` can just be an empty file .... After that you can simply do `from test import config`. Read more here https://docs.python.org/3/tutorial/modules.html – Mazdak Apr 30 '18 at 12:59
  • Still no luck, I've added in the empty files on the directory level but the same errors are popping up. Does seem to stick at module not found error – Jake Bourne Apr 30 '18 at 13:37
  • 1
    You are still getting the ImportError and ModuleNotFoundError even after adding the __init__.py? Can you update your post to show how you added __init__.py and also the updated import codes? – Gino Mempin May 01 '18 at 03:42
  • **No**, `__init__.py` is **not** needed. That is misinformation. I essentially never create `__init__.py` files unless I need them to actually contain some magic (e.g. `__all__`), and it does not cause problems. The problem here is caused by unrelated typos: trying to access a `debug` attribute that doesn't exist, and trying to treat the class interchangeably with the module that contains it. – Karl Knechtel Jan 19 '23 at 08:03

1 Answers1

2
#test.py

class Test:
    try:
        # Trying to find module in the parent package
        from . import config
        print(config.Config.debug)
        del config
    except ImportError:
        print('Relative import failed')

    try:
        # Trying to find module on sys.path
        import config
        print(config.Config.debug)
    except ModuleNotFoundError:
        print('Absolute import failed')


#config.py

class Config:
    debug = True
    def __init__(self, name):
        self.name = name

#config.py

In this file, the error was appearing because there was no variable debug = True

class Config:
    debug = True
    def __init__(self, name):
        self.name = name

#test.py

In this file, the error was appearing because you were executing the print of the import of the file and trying to access the variable in a direct way, that is, you would need 3 steps... import, class, variable >>> config.Config.debug

print(config.Config.debug)

I hope it helped you