当我尝试运行 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).
我甚至尝试删除来自,但这仍然会给我带来错误。
答案 0 :(得分:0)
virtualenv test_env
创建了一个python环境。 source test_env/bin/activate
来获取我的环境。python -m pip install "django<2"
安装django。python -m django --version
提供1.11.9
django-admin startproject mysite
。这直接来自django projects tutorial。打开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)