A helpful ImportError for manage.py when Django isn't installed/available

8,398 views
Skip to first unread message

Tim Graham

unread,
Mar 31, 2016, 1:15:05 PM3/31/16
to Django developers (Contributions to Django itself)
Ben Welsh (palewire) raised a proposal on a GitHub pull request [0]:

I've seen newbies flounder when they receive this error after running manage.py.

$ python manage.py
Traceback (most recent call last):
  File "manage.py", line 7, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

The root cause is almost always simple, like forgetting install Django with pip or neglecting to "activate" a virtual environment. But the Python jargon doesn't do much to help people new to our world connect the dots.

My proposal: Catch that error and print out a more verbose message that explains to the user exactly what's wrong. Here's my draft. I'd welcome any changes, but I think something along these lines could better welcome new people into Django.
          
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    installed and available on your PATH variable?")
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?



---------------------
Claude says, "I'm not convinced about this. Aren't we hiding other possibly helpful import errors (at least on Python 2)?"
Aymeric says, "I share Claude's concern. We've been constantly removing that sort of "helpful" exception wrapping from Django"
Aymeric again, after further investigation, "Importing django.core.management doesn't ripple too far. Specifically it doesn't import any of the user's code. This reduces the likelihood of masking useful errors. It will mask exception info if Django is installed incorrectly, for instance because two installs happened in the same location (but I think that's less likely since pip/virtualenv became mainstream and since we added code to setup.py to detect existing installs).

Does anyone else have opinions on the change? I suppose another option could be to try to reraise the original exception with the "helpful message" added.

Shai Berger

unread,
Apr 1, 2016, 6:23:55 AM4/1/16
to django-d...@googlegroups.com
An idea: Catch the exception; in the handler, try to simply "import django".
If this works, reraise, if it fails, print helpful message.

Collin Anderson

unread,
Apr 3, 2016, 1:33:48 PM4/3/16
to django-d...@googlegroups.com
We could also wait til after we remove py2 support in January. That way we can be sure that on py3 it will also print the original exception so it doesn't get hidden. (Right?)

Rafael Henter

unread,
Apr 7, 2016, 4:33:44 PM4/7/16
to Django developers (Contributions to Django itself)
Tim Graham,

How do you install django? Are you using virtualenv ?

Best,

Rafael Henter

Tim Graham

unread,
Apr 7, 2016, 6:41:39 PM4/7/16
to Django developers (Contributions to Django itself)
Yes, I always install Django in a virtualenv rather than system-wide. I'm sure there are some users installing it system-wide, but I think that's more and more a niche use case.

Tim Graham

unread,
Apr 9, 2016, 1:33:34 PM4/9/16
to Django developers (Contributions to Django itself)
I implemented Shai's suggestion here: https://github.com/django/django/pull/6438

I think we can drop that extra complication when dropping Python 2.
Reply all
Reply to author
Forward
0 new messages