3

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

Hanny
  • 580
  • 3
  • 16
  • 44
  • Can you import it in your python shell? – xyres May 16 '18 at 19:10
  • Yes - I can import it in python shell. – Hanny May 16 '18 at 19:10
  • And this only happens when you run `coverage`? Everything works fine when you run your tests or django server otherwise? – xyres May 16 '18 at 19:20
  • That's correct. If I run `python manage.py test visitor_check_in` the tests all run and pass without issue. – Hanny May 16 '18 at 19:23
  • 1
    It could be possible that `coverage` is not using your virtualenv, but your global python installation instead. Although I'm not sure about that. But there's been a [similar issue](https://stackoverflow.com/q/18959072/1925257) posted before. – xyres May 16 '18 at 19:26
  • It would appear you are correct. I must have missed that in looking at my `pip freeze` - I was banging my head against this for some time. Thank you! Please put this as the answer so I can make sure you get your points! – Hanny May 16 '18 at 19:37
  • try, direnv it is good as it maintains virtualenv per directory. – Vinay P May 16 '18 at 19:46
  • @Hanny Added an answer. Thanks for the incentive! – xyres May 16 '18 at 20:08

1 Answers1

6

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.

xyres
  • 20,487
  • 3
  • 56
  • 85
  • Also, uninstall coverage from your global python installation. Even if coverage is installed in the virtualenv, you may still be running the one installed in the global python installation. The global one, if run, will ignore your virtualenv. – erikvw Sep 18 '19 at 20:55