我在Windows上的Wing IDE内部运行PyLint。我有一个子目录(包)在我的项目和包内,我从顶层导入一个模块,即。

__init__.py
myapp.py
one.py
subdir\
    __init__.py
    two.py

在two.py中,我导入了一个,这在运行时工作得很好,因为顶层目录(myapp.py从其中运行)在Python路径中。然而,当我在two.py上运行PyLint时,它会给我一个错误:

F0401: Unable to import 'one'

我怎么解决这个问题?


当前回答

I have been struggling with this a lot and eventually found out something that was not described here. Here are my 2 cents to this issue: I am using VS Code on Windows, using virtual env. For some reasons, the pylint executable is called epylint and not pylint. In my script or from CLI prompt, I was running pylint ./xxx and the system was launching a pylint it found somewhere else but not the appropriate one. I just added an e in my shell script and my 'Unable to import' issues eventually vanished.

其他回答

我刚刚发现的一个解决办法是,实际上只对整个包运行PyLint,而不是对单个文件运行PyLint。以某种方式,它设法找到导入的模块。

这是一个老问题,但没有公认的答案,所以我建议这样做:将import语句更改为two.py:

from .. import one

在我当前的环境中(Python 3.6, VSCode使用pylint 2.3.1),这将清除标记语句。

首先,进入VS Code,然后按“ctrl + shift + p”

然后搜索settings.json

然后将下面的代码粘贴到设置中。我希望问题能够解决。

{

"python.pythonPath": "venv/bin/python",
"python.linting.pylintPath": "venv/bin/pylint"

}

这些都对我有用!

.pylintrc

[MASTER]
; init-hook='import sys; sys.path.append("./venv/lib/python3.8/site-packages")'

; or
init-hook='import sys; sys.path.append(f"./venv/lib/python{sys.version[:3]}/site-packages");'

; or
;init-hook='from distutils.sysconfig import get_python_lib; sys.path.append(get_python_lib())'

; or
; init-hook='import site; sys.path.append(site.getsitepackages()[0])'

编辑~ /。Pylintrc来包含你的模块上面的目录,像这样:

[MASTER]
init-hook='import sys,os;[sys.path.append("your_workspace_path"+di) for di in os.listdir("your_workspace_path")]'