简体   繁体   English

VSCode无法导入“示例” pylint(导入错误)

[英]VSCode Unable to import 'example' pylint(import-error)

I am getting pylint errors in VSCode that say they are unable to import local files. 我在VSCode中收到pylint错误,提示它们无法导入本地文件。 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. 我以为pylint使用的是解释器使用的PYTHONPATH,所以我不知道为什么会这样。

I have my code set up like so: 我的代码设置如下:

dir0
-dir1
--__init__.py
--src
---__init__.py
---srcdir1
----__init__.py
----file1.py
---srcdir2
----__init__.py
----file2.py

file1.py looks like this: file1.py看起来像这样:

def func1():
    return 1

file2.py looks like this: file2.py看起来像这样:

from srcdir1.file1 import func1
func1()

in launch.json I have: 在launch.json中,我有:

"env": {"PYTHONPATH": "/full/path/to/dir0/dir1/src:/usr/local/bin/python"}

Pylint is giving me an import error around "from srcdir1.file1". Pylint在“ 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. 但是,如果我右键单击并选择“运行代码”,则会收到与pylint错误匹配的导入错误。

EDIT: I created a file in my workspace folder called .env in my workspace folder. 编辑:我在我的工作区文件夹中的工作区文件夹中创建了一个名为.env文件。 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

For some reason, import sys (but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import) 出于某种原因, import sys (而不是其他)没有给我以下错误: unresolved import 'sys'Python(unresolved-import)

Do you have __init__.py files inside those folders? 这些文件夹中是否有__init__.py文件? Otherwise python won't recognise them as modules and will be unable to import the code. 否则,python将无法将它们识别为模块,并且将无法导入代码。 Have a look at https://stackoverflow.com/a/448279/5015356 for more information 看看https://stackoverflow.com/a/448279/5015356了解更多信息

The problem is that you specified a PYTHONPATH for the debugger and not the general extension to send to Pylint. 问题是您为调试器指定了PYTHONPATH ,而不是要发送给Pylint的常规扩展名。 Try setting PYTHONPATH in a .env environment variable definition file . 尝试在.env环境变量定义文件中设置PYTHONPATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM