SyntaxError:无效的语法manage.py,除了ImportError除外:

I have created a django project called tictoctoe but when I run "py manage.py runserver" it gives me following error.

File "manage.py", line 11
    except ImportError as exc:
                        ^
SyntaxError: invalid syntax

它以前可以正常工作。

这是

manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tictactoe.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)


if __name__ == '__main__':
    main()
评论
  • 你无可取代
    你无可取代 回复

    请确保在安装Django的地方激活了虚拟环境。

  • 甜小妞
    甜小妞 回复

    我可以找出问题所在,

    我已经在虚拟环境中创建了项目目录,因此,如果不激活虚拟环境,则会出现该错误。

    激活venv后,py manage.py runserver可以正常工作。

    如何激活虚拟环境:

    my project location:
    c:\Users\xyz\Documents\django-projects
    
    my virtual environment location is 
    c:\Users\xyz\Documents\django-projects\django-env
    
    go to 
    c:\Users\310227436\Documents\django-projects\django-env\Scripts 
    
    invoke activate:
    c:\Users\310227436\Documents\django-projects\django-env\Scripts> Activate
    

    做完这个问题后应该解决。