coveragepy: ModuleNotFoundError: No module named 'django_project' when I used coverage run manage.py test

The below issue is with Coverage version 5.0a7

When trying to run test for my django project using coverage run manage.py test i get ModuleNotFoundError. This issue kept coming and i thought it was as a result of what was addressed here --> https://stackoverflow.com/questions/50378166/django-coverage-modulenotfounderror-no-module-named-django-extensions

None of the recommendations worked, i downgraded my django version from 3.0 to 2.2 and the problem persisted. This issue was rectified by downgrading to coverage version 4.5

Like i said, i only encountered this issue with coverage version 5.0a7

With the below dependencies, coverage is working as expected

λ pipenv run pip list
Package          Version
---------------- -------
asgiref              3.2.3
coverage          4.5
Django              2.2
Pillow               6.2.1
pip                   19.3.1
psycopg2         2.8.4
pytz                2019.3
setuptools       41.4.0
sqlparse         0.3.0
wheel            0.33.6

complete error log

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\base.py", line 320, in run_from_argv
    parser = self.create_parser(argv[0], argv[1])
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\base.py", line 294, in create_parser
    self.add_arguments(parser)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\core\management\commands\test.py", line 44, in add_arguments
    test_runner_class = get_runner(settings, self.test_runner)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\test\utils.py", line 301, in get_runner
    test_runner_class = test_runner_class or settings.TEST_RUNNER
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\site-packages\django\conf\__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "c:\users\spaceofmiah\.virtualenvs\backend--o8je5ax\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'newproject'

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 40 (15 by maintainers)

Commits related to this issue

Most upvoted comments

For those facing this problem: a work-around seems to be to use “python -m coverage run (etc)” instead of “coverage run (etc)”.

@jpmckinney Thanks, I was finally able to reproduce it myself. Ironically, this turned out to be the important bit:

# I don't think it matters that I use pyenv. Create the virtualenv however you want.
pyenv virtualenv coveragepy
pyenv activate coveragepy

It wasn’t until I used pyenv and pyenv-virtualenv that the problem appeared for me. Now to work on a fix…

This is now available in 5.0.3: https://pypi.org/project/coverage/5.0.3/

I was into same error for me it was just because of running in a lot of errors i just unintentionally did some typo or changed the app name in Procfile and now it works fine.

I have what I think is a fix for this problem. If people could try it and let me know, that would be a huge help:

pip install git+https://github.com/nedbat/coveragepy@nedbat/fix-862#egg=coverage==0.0

/cc @hugovk @tritium21 @rly @jpmckinney @spaceofmiah @Temeez @mpbrown @obi-jerome

Same problem here. Python 3.6.8 [MSC v.1916 64 bit (AMD64)] on win32 No Problem with coverage==4.5.4 Fail with coverage==5.0.2 When I look at this thread, I see that @spaceofmiah runs Windows but @nedbat does not. @mpbrown, do you use Windows too ?