Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError - tests failing on CircleCI but pass locally

Exactly as title says - my folder structure is something like this:

venv/  
__init__.py  
.circleci/  
    config.yml  
Dockerfile   
docker-compose.yml  
config.py  
requirements.txt  
src/  
    __init__.py  
    other_scripts.py  
tests/  
    __init__.py  
    test_a.py  
    test_b.py  

the test files have a from config import * line. Running $ pytest from the root directory locally or through a bash shell into the container (inside a virtualenv) works as expected, but on CircleCI the build fails with an ImportError: No module named 'config' for the above line of code. I'm using python3.5 and circleCI 2.0.

Thanks in advance!

like image 330
SquidwardsRevenge1996 Avatar asked Nov 07 '22 04:11

SquidwardsRevenge1996


1 Answers

As suggested in the comment: remove __init__.py from the root dir, add an empty file named conftest.py.

For the explanation of the conftest.py trick, take a look at my other answers to similar questions, for example pytest cannot find module or Using pytest with a src layer. In short, adding a conftest.py will add the project dir to sys.path, so the config module becomes importable.

As for removal of __init__.py file, it's not related to the error, it just doesn't belong there. Surely, you don't want to make the project dir to a package, so an early removal of unneeded init module will spare you some unexpected errors in the future.

like image 149
hoefling Avatar answered Nov 14 '22 21:11

hoefling