VSCode Unable to import 'example' pylint(import-error)

落花浮王杯 提交于 2020-06-27 16:32:07

问题


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.

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:

def func1():
    return 1

file2.py looks like this:

from srcdir1.file1 import func1
func1()

in launch.json I have:

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

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.

EDIT: 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

For some reason, import sys (but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import)


回答1:


Do you have __init__.py files inside those folders? Otherwise python won't recognise them as modules and will be unable to import the code. Have a look at https://stackoverflow.com/a/448279/5015356 for more information




回答2:


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.



来源:https://stackoverflow.com/questions/56784306/vscode-unable-to-import-example-pylintimport-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!