You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法导入django-smart-selects,Pylance提示导入路径无法解析

Django Smart Selects 导入错误解决方法

问题描述

使用django-smart-selects创建关联下拉框时,已按库文档完成全部配置,但出现Pylance导入错误:
import "smart_selects.db_fields" could not be resolved Pylance(reportMissingImports) [Ln2, Col6]
导入语句中的库名呈灰色,未被编辑器识别。

当前配置信息

settings.py

INSTALLED_APPS = [
    'routes_form.apps.RoutesFormConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'smart_selects',
]

USE_DJANGO_JQUERY = True

urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('form/', include('routes_form.urls')),
    path('admin/', admin.site.urls),
    path(r'^chaining/', include('smart_selects.urls')),
]

models.py

from django.db import models
from smart_selects.db_fields import ChainedForeignKey

已尝试将JQUERY_URL = True修改为USE_DJANGO_JQUERY = True,但错误未解决,仅存在上述导入识别问题。

解决方法

该错误是由于Pylance未正确识别django-smart-selects的模块路径导致,可通过配置编辑器的额外路径解决:

  • 打开VS Code设置(快捷键Ctrl+,),搜索Python > Analysis: Extra Paths,点击编辑 in settings.json
  • 在settings.json中添加django-smart-selects库所在的实际路径,虚拟环境下示例路径如下:
    "python.analysis.extraPaths": [
        "${workspaceFolder}/venv/lib/python3.11/site-packages/smart_selects"
    ]
    
    注意替换路径中的Python版本号为你实际使用的版本
  • 保存设置后重启VS Code,Pylance即可正确识别导入路径。

内容的提问来源于stack exchange,提问作者Oleksandr Lager

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 01:35:26