问题very similar to this one已经存在,但它与python2有关。 In another related question用户未激活其虚拟环境。我做到了。
在一个更大的项目中遇到这个问题之后,我尝试并成功地在一个全新的项目中复制它。
在空文件夹test-django中,我运行了以下内容:
virtualenv env
env/scripts/activate
pip install django
为了确保python已经成功安装了django,我试过了:
python
import django
没有引发导入错误,并且运行的Python版本是 Python 3.6.3 (v3.6.3:2c5fed8,2017年10月3日,18:11:49)
所以我开始了这个项目......
django-admin startproject test
项目测试确实成功创建。然后我跑了:
cd test
./manage.py
得到了
文件“test-django \ test \ manage.py”,第14行 来自exc ^ SyntaxError:语法无效
manage.py的内容是:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "phoenix2.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)
当我删除“from exc”时,ImportError被成功引发 - 但我不应该从安装了django的虚拟环境中运行一个ImportError。
我尝试运行(在命令行中使用Python)
from django.core.management import execute_from_command_line
并没有得到ImportError
虚拟环境中sys.path的内容:
['','\ test-django \ env \ Scripts \ python36.zip','\ test-django \ env \ DLLs','\ temp \ test-django \ env \ lib','\ test- django \ env \ Scripts','c:\ python36 \ Lib','c:\ python36 \ DLLs','\ test-django \ env','\ test-django \ env \ lib \ site-packages'] < / p>
运行django-admin --version:2.0.5
如果它有用,这里是一般系统python的路径内容:
['','C:\ Python36 \ python36.zip','C:\ Python36 \ DLLs','C:\ Python36 \ lib','C:\ Python36','C:\ Python36 \ lib \站点包]
在virtualenv中冻结pip:
的Django == 2.0.5 pytz == 2018.4
pip冻结一般python:
appdirs == 1.4.3 LXML == 4.0.0 包装== 16.8 pyparsing == 2.2.0 6 == 1.10.0 的virtualenv == 15.1.0
答案 0 :(得分:0)
如何运行python3 manage.py
代替./manage.py
?
我建议使用pipenv
代替virtualenv + pip。
答案 1 :(得分:0)
This is the simple way to start new django project:-
In terminal do following steps
$ mkvirtualenv <env_name>
$ workon <env_name>
$ pip install django
$ django-admin startproject <project_name>
$ cd <project_name>
$ ./manage.py migrate
$ ./manage.py runserver
open this in browser http://127.0.0.1:8000/
open the project in pychram and then do this
click on file -> settings -> projects interpreter -> add local -> select <env_name> -> click bin -> click python3 or python2 -> apply -> OK
答案 2 :(得分:-1)
通过运行python ./manage.py
而非./manage.py