【问题标题】:Import cannot be resolved pylance error in vscode, have tried a few fixes导入无法解决vscode中的pylance错误,已经尝试了一些修复
【发布时间】:2025-11-27 19:20:08
【问题描述】:

我在为我的 Django 应用程序使用 pylint 时遇到问题。我收到了一些错误,并已使用其他线程解决了一些错误,但我仍然遇到的主要错误是无法解决导入“.forms”

当我尝试使用以下导入 from .forms import ArtistForm(ArtistForm 是一个类)从 forms.py 导入时,我在 views.py 文件中遇到了这个错误。

我已经尝试过以下资源,但不是很成功。

这是我添加到我的 .vscode/settings.json 以尝试解决上述问题 -

"python.analysis.extraPaths": ["tunr/", "./tunr/templates/tunr"]

我还尝试使用下面文章中详述的 .env 修复 -

我是编码新手,所以我一直无法完全理解如何解决这个问题。任何帮助表示赞赏。

下面是我的文件结构,不是tunr文件夹在根目录下:

tunr
     ├── apps.py
     ├── models.py
     ├── templates
     │   └── tunr
     │       ├── artist_detail.html
     │       ├── artist_form.html
     │       ├── artist_list.html
     │       ├── base.html
     │       ├── forms.py
     │       ├── song_detail.html
     │       └── song_list.html
     └── views.py

如果有任何其他信息有帮助,请告诉我。

【问题讨论】:

  • 您的表单在templates文件夹中,请将其移至tunr文件夹,即可解决问题
  • @SanthoshReddy 非常感谢。多么容易解决。感谢您的帮助,我感到非常沮丧。
  • 我会搬过来回答的

标签: python django visual-studio-code import pylint


【解决方案1】:
tunr
     ├── apps.py
     ├── models.py
     ├── forms.py 
     ├── templates
     │   └── tunr
     │       ├── artist_detail.html
     │       ├── artist_form.html
     │       ├── artist_list.html
     │       ├── base.html
     │       ├── song_detail.html
     │       └── song_list.html
     └── views.py

请改用以下树形结构,因为您在模板文件夹中编写表单时遇到此问题

【讨论】:

    最近更新 更多