1. 程式人生 > >【Python】django報錯SyntaxError: from __future__ imports must occur at the beginning of the file解決方法

【Python】django報錯SyntaxError: from __future__ imports must occur at the beginning of the file解決方法

D:\PythonWorkstation\django\django_station\queryset>python manage.py makemigrations Traceback (most recent call last):   File "manage.py", line 15, in <module> 省略部門資訊.....   File "D:\PythonWorkstation\django\django_station\queryset\blog\models.py", line 2     from __future__ import unicode_literals     ^ SyntaxError: from __future__ imports must occur at the beginning of the file

解決辦法:

將from __future__ import ...這段語句放在程式碼塊最前面即可。

錯誤程式碼:

from django.db import models
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible

正確示例:

from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible