VSCode 无法导入“示例"pylint(导入错误) [英] VSCode Unable to import 'example' pylint(import-error)
问题描述
我在 VSCode 中收到 pylint 错误,提示他们无法导入本地文件.但是,我可以毫无问题地通过调试器运行这些文件.我认为 pylint 使用的 PYTHONPATH 与解释器使用的相同,所以我不知道为什么会发生这种情况.
I am getting pylint errors in VSCode that say they are unable to import local files. However, I am able to run the files through the debugger with no problem. I thought that pylint used the same PYTHONPATH that the interpreter uses, so I don't know why this is happening.
我的代码设置如下:
dir0
-dir1
--__init__.py
--src
---__init__.py
---srcdir1
----__init__.py
----file1.py
---srcdir2
----__init__.py
----file2.py
file1.py 看起来像这样:
file1.py looks like this:
def func1():
return 1
file2.py 看起来像这样:
file2.py looks like this:
from srcdir1.file1 import func1
func1()
在launch.json中我有:
in launch.json I have:
"env": {"PYTHONPATH": "/full/path/to/dir0/dir1/src:/usr/local/bin/python"}
Pylint 给我一个关于来自 srcdir1.file1"的导入错误.当我进入调试器并单击运行调试器时,文件运行没有问题.但是,如果我右键单击并选择运行代码",则会出现与 pylint 错误匹配的导入错误.
Pylint is giving me an import error around "from srcdir1.file1". When I go into the debugger and click run debugger, the file runs with no issues. However, if I right click and select Run Code, I get import errors that match the pylint errors.
我在我的工作区文件夹中创建了一个名为 .env
的文件.如下:
I created a file in my workspace folder called .env
in my workspace folder. It is as follows:
PYTHONPATH=/Library/Python/2.7/site-packages:/Users/user/path/dir0/dir1/src:/Users/user/path/client/src:/Users/user/path/product/src
有趣的是,我可以从产品(列表中的第三个)导入,但不能从客户端导入.有没有地方覆盖了这个环境?
Interestingly, I can import from product (the third in the list) but not from client. Is there somewhere that this environment is being overridden?
我的文件中还有以下内容:
I also have the following in the file:
import os
import shutil
import sys
出于某种原因,import sys
(但不是其他)给了我以下错误:unresolved import 'sys'Python(unresolved-import)
For some reason, import sys
(but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import)
推荐答案
问题是您为调试器指定了 PYTHONPATH
而不是要发送到 Pylint 的通用扩展.尝试在 PYTHONPATH
>.env 环境变量定义文件.
The problem is that you specified a PYTHONPATH
for the debugger and not the general extension to send to Pylint. Try setting PYTHONPATH
in a .env
environment variable definition file.
这篇关于VSCode 无法导入“示例"pylint(导入错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!