agconti agconti - 1 year ago 365
Python Question

ImportError: No module named rest_framework.authtoken

I'm using django rest-framework's, (DRF), token authentication in my project to create tokens when a user is created. Everything works great until I add this line from the DRF docs:

url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),

to create an endpoint that returns the token for a user after correctly posting the username and password.

It throws this error:

ImportError: No module named rest_framework.authtoken

This is strange because DRF is fine with out this line, so it must be included in my PYTHONPATH.

I've also run
python syncdb
and the appropriate migrations.

Any idea what could be wrong?

'south', # Database migration helpers:
'crispy_forms', # Form layouts
'avatar', # for user avatars
'rest_framework', # for rest
'django_rq', # for aysnc
'rest_framework_swagger', # for exploring the api

Full stack trace:

Unhandled exception in thread started by <function wrapper at 0x1046096e0>
Traceback (most recent call last):
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/", line 93, in wrapper
fn(*args, **kwargs)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/commands/", line 101, in inner_run
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/", line 196, in get_app_errors
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/", line 78, in _populate
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/", line 40, in import_module
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/", line 63, in <module>
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/", line 51, in patch_root_urlconf
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 480, in reverse
app_list = resolver.app_dict[ns]
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 310, in app_dict
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 273, in _populate
for name in pattern.reverse_dict:
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 296, in reverse_dict
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 285, in _populate
lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 229, in callback
self._callback = get_callable(self._callback_str)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/", line 32, in wrapper
result = func(*args)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/", line 100, in get_callable
not module_has_submodule(import_module(parentmod), submod)):
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/", line 40, in import_module
ImportError: No module named rest_framework.authtoken

Answer Source

Check out the last response here. Instead of including the whole view as a string, import 'obtain_auth_token' first and then just refer to that.

from rest_framework.authtoken.views import obtain_auth_token

url(r'^api-token-auth/', obtain_auth_token),

Further update from agconti:

This issue stems from using:

urlpatterns = patterns("api.views",
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),

because of the views prefix. If you want to use api-token-auth/ this way you must change it to below or use the solution provided by Alex:

urlpatterns = patterns('',
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download