Django中的migrate.py文件出错

时间:2018-01-29 22:07:48

标签: python django

当我尝试运行 python manage.py runserver

时,我不断收到以下错误
File "manage.py", line 14
) from exc 
     ^
SyntaxError: invalid syntax

根据其他评论,我激活虚拟环境,并在Python 2.7.10和Django 1.11.9版本上运行。

下面是我的manage.py程序。

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dataContractCreator.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise 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?"
    ) from exc
    execute_from_command_line(sys.argv).

我甚至尝试删除来自,但这仍然会给我带来错误。

1 个答案:

答案 0 :(得分:0)

  1. 我用virtualenv test_env创建了一个python环境。
  2. 我使用source test_env/bin/activate来获取我的环境。
  3. 我使用python -m pip install "django<2"安装django。
  4. python -m django --version提供1.11.9
  5. 运行django-admin startproject mysite。这直接来自django projects tutorial
  6. 打开mysite/manage.py。你看:

    #!/usr/bin/env python
    import os
    import sys
    
    if __name__ == "__main__":
    
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
        try:
            from django.core.management import execute_from_command_line
        except ImportError:
            # The above import may fail for some other reason. Ensure that the
            # issue is really that Django is missing to avoid masking other
            # exceptions on Python 2.
            try:
                import django
            except ImportError:
                raise 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?"
                )
            raise
        execute_from_command_line(sys.argv)