ImportError from tests directory in Python

I have the following project structure:

python_project
├── module
│   ├── constants.py
│   └── __init__.py
├── scripts
│   ├── __init__.py
│   └── script.py
└── tests
    ├── constants.py
    └── __init__.py

python_project is in PYTHONPATH.

module/constants.py:

VAR_MODULE = 25

tests/constants.py:

VAR = 17

I'm facing the following issue in script.py file:

from module.constants import VAR_MODULE

works

from tests.constants import VAR

throws an exception:

    from tests.constants import VAR
ImportError: cannot import name 'VAR'

I know that there is no point in importing stuff from tests directory, just wondering why this does not work. Is tests directory excluded somehow?

Thanks!

A



Read more here: https://stackoverflow.com/questions/66278605/importerror-from-tests-directory-in-python

Content Attribution

This content was originally published by Alexey Abramov at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: