首先初始化一个 django 项目
django-admin startproject xxx
cd xxx
之后运行 django 项目 python manage.py runserver
时报错:
Traceback (most recent call last):
File "manage.py", line 11, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the
following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 17, in main
) from exc
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?
这里困惑了一下,django 项目都可以初始化,难道还能是 django 没安上?稳期起见还是检查了一下,发现没有问题
django-admin --version
3.2.5
问题应该是电脑中安装了多个 Python 版本,这里发生了冲突,推荐使用 virtualenv
pip install virtualenv
virtualenv env
激活 virtualenv
可以先用 cd env/bin & ls
或 tree env/Scripts
命令看一下 activate 在哪个文件夹里面(bin文件夹还是Scripts文件夹)
# 我这个版本的 activate 在 Scripts
source env/Scripts/activate
# 以前的版本的 activate 在 bin
source env/bin/activate
python manage.py runserver
deactivate