lundi 29 juin 2020

Relative imports in python 3.8 failing with ImportError: attempted relative import with no known parent package

I am just getting started with Test Driven Development with python and I am having some problems while importing the required files, relative to the current directory. Here's my directory structure:

project/
    api/
    utils/
        __init__.py
        database/
            dbms.py
            groups.py
            __init__.py
       //etc etc
    tests/
        db_test.py

From the db_test.py I want to import the dbms.py file. I tried using relative import like this:

from ..utils.database import dbms

But it fails with this error message:

Traceback (most recent call last):
  File "d:\Scola\tests\db_test.py", line 2, in <module>
    from ..utils.database import dbms
ImportError: attempted relative import with no known parent package

I went through this answer here and added empty __init__.py files to the root directory of the project as well as the tests directory. But the same error is still there. And then I tried using this, but then I got the error that 'utils module not found'. Any ideas?

Thanks.

Aucun commentaire:

Enregistrer un commentaire