PyCharm+Django Rest Framework环境配置及报错处理
1.启动服务报错
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?
python manage.py runserver
报错没有django:
ModuleNotFoundError: No module named 'django'
安装django
环境配置报错:Pipenv excutable is not found 问题
安装Pipenv
激活虚拟环境:conda activate annotation
执行:pip install pipenv
然后重新执行poetry install,再启动服务。
2.字符编码报错
ValueError: source code string cannot contain null bytes 原因是生成的py文件的字符编码不是通用的utf-8类型
排查到通过以下命令生成的models.py文件不是utf-8格式,重新存为utf-8格式即可。
python manage.py inspectdb > data/models.py
3.You may need to add '127.0.0.1' to ALLOWED_HOSTS
Invalid HTTP_HOST header: '127.0.0.1:8000'. You may need to add '127.0.0.1' to ALLOWED_HOSTS!
settings.py中做如下配置:
ALLOWED_HOSTS = ['xx.xx.xx.xx', '127.0.0.1']
https://www.cnblogs.com/JIM-FAN/p/14679629.html
4.配置调试环境
(1)查看Settings中的python环境配置
(2)调试模式的配置
打开:Run -> Edit Configurations...
点击左侧菜单Python/manage,在右侧窗口的Parameters配置manage.py脚本的参数 runserver
(这样相当于执行命令:python manage.py runserver)
配置python环境,选择与Settings中的python环境配置相同的版本,点击Apply,Ok保存配置。
配置完毕,点击Debug按钮即可调试。
5. 创建虚拟环境
有时候需要为不同的项目创建单独的虚拟环境。
查看虚拟环境:conda env list
(1)创建环境:https://blog.csdn.net/SARACH_WONG/article/details/89328307
cd 到项目目录,执行命令: conda create -n annotation python=3.10
(2)激活环境:https://blog.csdn.net/GW_wg/article/details/89810478