pythonredis

Getting an ImportError while trying to import from __init__.py


NOTE: Sorry if i dont format this correctly or something, Im newer to asking questions on stack overflow

Some of my code:

"""Server tools script"""

from typing import Dict
from types import FunctionType
from . import REDIS


# The rest of it

This error is what i am getting from the code:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\ever\Documents\overgames\app.py", line 11, in <module>
    main()
  File "C:\Users\ever\Documents\overgames\app.py", line 7, in main
    import servertools as dev
  File "C:\Users\ever\Documents\overgames\servertools.py", line 6, in <module>
    from . import REDIS
ImportError: attempted relative import with no known parent package

I Tried from overgames import REDIS but didnt work


Solution

  • import sys
    sys.path.append('..')
    from overgames import REDIS
    

    by including the parent directory in sys.path, you can use an absolute import for REDIS