5

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!

1 Answers1

1

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.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • is this only valid for pytest or also unittest modules? I'm also getting `Failed to import test module` – DAG Mar 04 '20 at 07:52