我正在做一个Django项目。我希望能够检查我的项目中的类型。为此,我安装了django-存根和类型。此外,我希望它能与VSCode扩展一起工作。它节省了时间,因为我不必每次都要检查所有的东西时,都要在终端机上运行回传。然而,MyPy只是因为找不到Django设置而崩溃……我已经尝试过在mypy.ini文件中移动。这没什么用。顺便说一句,如果我在终端上运行MyPy,它就能工作。
错误:ModuleNotFoundError: No module named 'my_project.settings'
mypy.ini:
[mypy]
plugins = mypy_django_plugin.main, mypy_drf_plugin.main
[mypy.plugins.django-stubs]
django_settings_module = "my_project.settings"settings.json:
{
"python.linting.mypyEnabled": true,
"python.linting.enabled": true,
"mypy.runUsingActiveInterpreter": true,
"mypy.configFile":"my_project/mypy.ini",
"mypy.targets":[
"."
]
}wsgi.py:
"""
WSGI config for web_app project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
application = get_wsgi_application()myvenv
|
|
.vscode
| |--settings.json
|
my_project
| |--mypy.ini
| |--my_project
| |
| |--settings.py
|
|发布于 2022-05-25 22:36:11
所以,解决办法并不明显,但我明白了。要解决这个问题,您需要将mypy.ini文件一个文件夹移回,并将mypy_path = ./my_project行包含到mypy.ini中。将此配置包括在[mypy]部分下。
我在这里找到了解决方案的想法:
https://github.com/typeddjango/django-stubs/issues/134
享受VSCode与MyPy。我希望以后会有更好的文档!
https://stackoverflow.com/questions/72370483
复制相似问题