首页 > 解决方案 > “未解决的导入”-错误:在 VSCode 中包括代码完成和 pylint 的路径

问题描述

为了有代码完成和功能 linting,我希望 VSCode 包含一个特定的路径。

https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file

我创建了一个测试环境

$ pwd
==> /Users/stephan/saitc/test_ws

$ ls -a
==> .           .env            tst_pckg
==> ..          __init__.py
==> .DS_Store   testing.code-workspace

$ cat .env
==> PYTHONPATH="/Users/stephan/saitc"

$ ls tst_pckg/
==> CONST.py        __init__.py     testing.py

$ cat tst_pckg/CONST.py
==> # -*- coding: utf-8 -*-
==> 
==> TEST_CONSTANT = "Test constant's value"

$ cat tst_pckg/testing.py
==> # -*- coding: utf-8 -*-
==> 
==> from test_ws.tst_pckg.CONST import TEST_CONSTANT

设置“python.envFile”设置为“${workspaceFolder}/.env”

有了这个设置,我希望 VSCode 知道包“test_ws”,但输入以下行:

from test_ws.tst_pckg.CONST import TEST_CONSTANT

在文件“testing.py”中给出了我没有合理的代码完成并且“test_ws.tst_pckg.CONST”无法解决(VSCode ui 中的“未解析导入”-错误)。

我在 macos 上使用 Anaconda 和它附带的 Visual Studio Code 1.30.1。难道我必须从目录“/Users/stephan/saitc/test_ws”启动visual studio代码吗?(如果有怎么办?)

每次启动 Visual Studio Code 时,都会询问我是否要启动 pylint 扩展。我怎样才能永久打开它?(Python›Linting:启用 Pylint,Python›Linting:启用)

谢谢,

斯蒂芬

标签: visual-studio-code

解决方案


您可以使用配置"python.autoComplete.extraPaths": ["./your-source/folder"]

据我了解,env 文件仅在您启动终端等时使用,但不适用于扩展。


推荐阅读