0

I'm following the flaskr tutorial here, and I'm struggling with running tests.

    ➜  pytest
    ImportError while loading conftest 'flaskr_app/tests/conftest.py'.
    tests/conftest.py:5: in <module>
    from ..flaskr import create_app
    E   ImportError: attempted relative import with no known parent package

Pytest seems to be complaining about relative imports. Here is how I set up the import in conftest.py:

    import os
    import tempfile
    
    import pytest
    from ..flaskr import create_app
    from ..flaskr import get_db, init_db

Here's how my project directory looks like.

    ➜  tree
    .
    ├── flaskr
    │   ├── MANIFEST.in
    │   ├── __init__.py
    │   ├── auth.py
    │   ├── blog.py
    │   ├── db.py
    │   ├── schema.sql
    │   ├── static
    │   │   └── style.css
    │   └── templates
    │       ├── auth
    │       │   ├── login.html
    │       │   └── register.html
    │       ├── base.html
    │       └── blog
    │           ├── create.html
    │           ├── index.html
    │           └── update.html
    ├── instance
    │   └── flaskr.sqlite
    ├── setup.cfg
    ├── setup.py
    └── tests
        ├── conftest.py
        └── data.sql

Since the flaskr directory resides in the parent directory of tests directory, I thought it would be appropriate to import the package from ..flaskr.

Can anyone tell me what's causing this error?

davidism
  • 121,510
  • 29
  • 395
  • 339
ric.row
  • 505
  • 1
  • 6
  • 18
  • [Please try](https://meta.stackoverflow.com/questions/261592) to look for existing solutions before posting a question, for example by [copying and pasting error messages into a search engine](https://duckduckgo.com/?q=attempted+relative+import+with+no+known+parent+package). – Karl Knechtel Oct 12 '22 at 02:48
  • Solution: No need for relative imports, since `flaskr` is installed as pip package. Just use `import flaskr` – ric.row Oct 12 '22 at 02:50

0 Answers0