mypy,vscode,django,modulenotfound

发布于 2025-02-01 00:47:21 字数 1302 浏览 0 评论 0原文

我正在研究一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-02-08 00:47:21

因此,解决方案并不明显,但我明白了。要解决它,您需要将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!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文