Django settings modules not found? ModuleNotFoundError

Hi, at my first contact with CI I run into troubles while setting up an test coverage analysis. My local test runs fine within it’s virtualenv:
coverage run test --settings .

But when I setup travis, I get an error:

ModuleNotFoundError: No module named ‘’

I tried various options and constellations, but it seems that I didn’t understand some aspects of the python path :frowning_face:

My project looks like:

├── papersquirrel
│   ├──
│   ├── __pycache__
│   ├── settings
│   │   ├──
│   │   ├──
│   │   ├──
│   │   ├──
│   │   └── __pycache__
│   ├──
│   └──
├── requirements.txt
├── res
├── squirrel
│   ├──
│   ├──
│   ├──
│   ├──
│   ├── management
│   │   └── commands
│   ├── migrations
│   │   ├──
│   │   └── __pycache__
│   ├──
│   ├── __pycache__
│   ├── templates
│   │   └── squirrel
│   ├── tests
│   │   └── basic-html.html
│   ├──
│   ├──
│   ├──
│   └──
├── static 

My .travis.yml is like this:

  -DJANGO=2.2 DB=sqlite3

# install requirements
  - pip install -r requirements.txt
  - pip install coveralls

# To run tests
  - coverage run --source=./ test

Can anybody with more env experience pls. point me to my misunderstanding?

My guess is: you need to add the current directory to Python path:

- export PYTHONPATH="$(pwd)"
- coverage <etc>

As per

you are running through coverage – a wrapper over a Python script. So the directory where that wrapper is located is added to sys.path instead of the current directory.

Unfortunatly, this doesn’t seem to solve my problem :slightly_frowning_face:
I added

  - export PYTHONPATH=$PYTHONPATH:$(pwd) 

and checked the variable via echo. It looks good and includes the directory of my project. Also just invoking python test results in this issue …

I also tried to rollout the repo on another server to avoid bugs due local artifacts, but also on that server everything works fine.
Sorry, but any further ideas?

Well, debugging is the further idea. It’s always the same:

  • Identify the exact place in the code where the error manifests itself
  • Examine the program’s state at that moment to find out which of the values is wrong
  • Track the wrong value back to its origin

You can insert debug printing into files if you know how to do that on the fly, or request debug mode for your project and edit the code by hand or run it under debugger.

Well, 40+ builds later and trying different ideas, but I didn’t solved the problem yet and I’m close to give up :sob:

First I just try to run only the testsuites itself, but n
All in all, this seems to be a problem with the import path, but I checked, that the project root folder and the settings subfolder are on the paths:

$ echo $PATH


python -c 'import sys; print(sys.path)'

['', '/home/travis/build/Matthias84/papersquirrel', '/home/travis/build/Matthias84/papersquirrel/papersquirrel/settings', '/home/travis/virtualenv/python3.6.7/lib/', '/home/travis/virtualenv/python3.6.7/lib/python3.6', '/home/travis/virtualenv/python3.6.7/lib/python3.6/lib-dynload', '/opt/python/3.6.7/lib/python3.6', '/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages']

If I skip the --settingsparameter, the suite runs as expected, but of course it doesn’t has the right settings to do it’s job.
I also tried different other approaches, that say it’s a problem of the travis virtualenv or to find my module itself:

  • switch to python3
  • invoke via python -m
  • create at root folder
  • try only instead
  • rename project folder to uniqe name

Any help would be appreciated :confused:

A check of file structure and sys.path at the time of import shows…

… that the 2nd check was unnecessary. There’s no ./papersquirrel/settings/ Apparently, you forgot to commit it…

Update: the link above is now dead. This is what I did there, roughly:

  - |-
    python -m pdb <path-to-coverage> <args> <<!
    b <faulty_file:faulty_line_that_does_the_import>
    import sys,os
    os.system("ls -Rl")
    p sys.path
1 Like

God damnit, you are right! :superhero: My .gitignore blocked the settings file and my test on another server was with a custom local settings file …
Sometimes it’s the most basic things … thank you very much!