I have read many previously asked questions about absolute and relative imports but I still cannot figure out how to solve the issue I have.
All my code is in the same directory contrary to some previously answered questions. I have added an empty __init__.py
as suggested in some answers but it also does not change my issue.
My app structure:
cli/
tests/
__init__.py (empty)
base.py
util.py
main.py
In base.py
, doing from util import magical_function
works (I can run my tests) but Pylint complains:
************* Module tests.base
W: 33, 0: Relative import 'util', should be 'tests.util' (relative-import)
I am running my tests using this script:
#!/usr/bin/env bash
CURRDIR="$(cd "$(dirname "$0")" && pwd)"
export PYTHONPATH=${CURRDIR}/../
source ${CURRDIR}/../activate
python ${CURRDIR}/../tests/main.py "$@"
activate
starts the virtual environment:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source ${DIR}/.virtualenv/bin/activate
source ${DIR}/.virtualenv/bin/postactivate
Following the instructions and having from tests.util import magical_function
results in an error when running the tests: ImportError: No module named util
.
How to change my import to have a working Python code and no errors found by Pylint (without disabling one of its checks)?