Devin Dixon
Devin Dixon

Reputation: 12383

Getting Started With DJango REST Framework SyntaxError: invalid syntax

I am new to python and even newer to DJANGO. I've written two Python app before but it free form. I've started learning DJANGO Rest Framework this: https://www.django-rest-framework.org/tutorial/quickstart/

Copying exactly I've made it up to Testing our API, and running python manage.py runserver gives me:

Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x10350c410>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Library/Python/2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 256, in check
    for pattern in self.url_patterns:
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 407, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Python/2.7/site-packages/django/urls/resolvers.py", line 400, in urlconf_module
    return import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/devindixon/Sites/Hearst/tutorial/tutorial/urls.py", line 18, in <module>
    from rest_framework import routers
  File "/Library/Python/2.7/site-packages/rest_framework/routers.py", line 25, in <module>
    from rest_framework import views
  File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 16, in <module>
    from rest_framework import exceptions, status
  File "/Library/Python/2.7/site-packages/rest_framework/exceptions.py", line 17, in <module>
    from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
  File "/Library/Python/2.7/site-packages/rest_framework/utils/serializer_helpers.py", line 8, in <module>
    from rest_framework.compat import unicode_to_repr
  File "/Library/Python/2.7/site-packages/rest_framework/compat.py", line 77, in <module>
    import django_filters
  File "/Library/Python/2.7/site-packages/django_filters/__init__.py", line 4, in <module>
    from .filterset import FilterSet
  File "/Library/Python/2.7/site-packages/django_filters/filterset.py", line 184
    def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
                                                  ^
SyntaxError: invalid syntax

Is this my error or a system error? Accorindg to the stacktrace, it only hits my code at /tutorial/tutorial/urls.py

from django.conf.urls import url, include
from rest_framework import routers <----THIS IS LINE 18
from tutorial.quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Upvotes: 0

Views: 1129

Answers (2)

edilio
edilio

Reputation: 1868

My two cents: As @9769953 and @Livonia said the version of django-filter you are using uses in https://github.com/carltongibson/django-filter/blob/master/django_filters/filterset.py#L184 a Python 3.x syntax not compatible with Python 2.7 so this version. The new syntax is specified on https://www.python.org/dev/peps/pep-3102/.

The error that your are getting is on line 184:

File "/Library/Python/2.7/site-packages/django_filters/filterset.py", line 184
def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
                                              ^

SyntaxError: invalid syntax

So:

pip install "django-filter<2.0"

will fix your issue

Upvotes: 1

Linovia
Linovia

Reputation: 20976

You are using Python 2.7 which is incompatible with the latest django-filter version as you can see here

Please consider downgrading:

pip install "django-filter<2.0"

Upvotes: 1

Related Questions