mypy,vscode,django,modulenotfound
我正在研究一个Django项目。我希望能够检查项目中的类型。为此,我安装了Django-stubs和Mypy。此外,我希望它与VSCODE扩展程序一起使用。它节省了时间,因为每次我必须检查所有内容时,我都不必在终端运行Mypy。但是,Mypy只是出于某种原因而崩溃,因为它找不到Django设置.....我已经尝试围绕mypy.ini文件移动。它无济于事。顺便说一句,如果我在终端运行Mypy,它可以工作。
错误: modulenotfounderror:no模块名为“ 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
|
|
I am working on a Django project. I want to be able to check the types in my project. For this purpose, I have installed django-stubs and mypy. Moreover, I want it to work with the VSCode extension. It saves time because I don't have to run mypy in terminal every time I have to check everything. However, MyPy simply crashes because it cannot find Django settings for some reason..... I have already tried moving around the mypy.ini file. It does not help. BTW, it works if I run MyPy in terminal.
Error: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
|
|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,解决方案并不明显,但我明白了。要解决它,您需要将mypy.ini文件移回一个文件夹,并将
mypy_path = ./my_project
line列入mypy.ini。在[mypy]
节下包括此配置。我在此处找到了解决方案的想法:
https://github.com/github.com/github.com/typeddjango/django/django/django/django/django -Stubs/essess/134
与Mypy一起享受Vscode。我希望他们稍后再包括更好的文档!!!
So, the solution was not obvious, but I got it. To solve it you need to move the mypy.ini file one folder back and include the
mypy_path = ./my_project
line into the mypy.ini. Include this configuration under the[mypy]
section.I found the idea for the solution here:
https://github.com/typeddjango/django-stubs/issues/134
Enjoy the VSCode with MyPy. I hope they will include better docs later!!!