Hanny
Hanny

Reputation: 682

Django Coverage ModuleNotFoundError: No module named 'django_extensions'

I've dug through SO posts, I've dug through random obscure blogs and I can't seem to fix my issue here.

This is how I went about all this:

I created a nice fresh new virtual environment:

virtualenv venv

I installed all my requirements:

pip install -r requirements.txt

Per the LocalFlavor Documentation I pip installed django-localflavor

But when I try to run coverage on my application per the django docs:

coverage run --source='.' manage.py test visitor_check_in

I get the error 'No module named 'localflavor'...

My installed apps looks like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admindocs',
    'localflavor',
    'visitor_check_in',
    'django_extensions',
]

Just last week I had this running, but I must have goofed something up during a conference recently when I was running through some tutorials or something - but I was in other virtual environments for those tutorials - so I'm stumped.

If I move the order of my installed apps like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admindocs',
    'visitor_check_in',
    'django_extensions',
    'localflavor',
]

It gives me the same no module found error - except it says it cannot find the module django_extensions

The traceback looks like this:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/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 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django_extensions'

As I said - before, I've had no problems with this, but now for the life of me I cannot dig out why this is not working. I have a sinking feeling there's something really simple I'm overlooking, but I can't find it.

I can confirm I'm using Python 3.6 and the latest version of pip

Edited to add: I can import the library in the Python shell with import localflavor

Upvotes: 4

Views: 5601

Answers (1)

xyres
xyres

Reputation: 21834

Since you mention that it is only happening when you run coverage it is quite possible that it is happening because coverage is not using your virtualenv, but your global python installation instead.

A question with similar issue has been posted here before - Running coverage inside virtualenv

Apparently, you need to install coverage in your virtualenv as well for it work as expected.

Upvotes: 7

Related Questions