PyLint无法识别Poetry环境:VSCode报E0401导入错误
VSCode中PyLint无法识别Poetry虚拟环境模块的解决方法
问题描述
VSCode无法识别Python脚本中导入的模块与库,每次导入都会提示Unable to import ### PylintE0401:import-error。Python脚本运行正常,每次都会通过poetry shell初始化并激活Poetry shell,且已在VSCode设置的「Python: Select Interpreter」中选择了激活的.venv环境,但PyLint仍指向错误解释器,不清楚该配置哪些设置。
相关命令输出:
poetry env list .venv (Activated). -> I'm choosing this environment in settings (Python: Select Interpreter) poetry env info --path /Users/makotomiyazaki/repos/Sakana/writing-assistant/.venv poetry config virtualenvs.path /Users/makotomiyazaki/Library/Caches/pypoetry/virtualenvs
解决方案
直接指定PyLint的绝对路径
打开VSCode设置(快捷键Cmd+,),搜索Python › Linting: Pylint Path,将其设置为Poetry虚拟环境内的pylint可执行文件绝对路径,示例路径:/Users/makotomiyazaki/repos/Sakana/writing-assistant/.venv/bin/pylint在项目配置文件中锁定环境
进入项目根目录下的.vscode/settings.json(没有则新建),添加以下配置,明确绑定虚拟环境解释器和PyLint路径:{ "python.pythonPath": "/Users/makotomiyazaki/repos/Sakana/writing-assistant/.venv/bin/python", "python.linting.pylintPath": "/Users/makotomiyazaki/repos/Sakana/writing-assistant/.venv/bin/pylint", "python.linting.pylintEnabled": true }重启VSCode加载配置
按Cmd+Shift+P调出命令面板,选择「Reload Window」,让新配置生效。验证Poetry环境关联
执行poetry run which pylint,确认输出路径与VSCode配置的一致。若不一致,重新执行poetry install同步环境依赖。
内容的提问来源于stack exchange,提问作者Makoto Miyazaki
相关产品推荐
相关产品推荐

