我在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'

我怎么解决这个问题?


当前回答

当我试图提交一个PR时,我得到了这个错误。我最终所做的只是在“import”发生的同一行上添加#pylint: disable=E0401。

这有助于我通过汽车考试。

其他回答

1)系统。Path是一个列表。

2)有时问题出在系统。Path不是你的virtualenv。路径,你想在你的virtualenv中使用pylint

3)就像上面说的,使用init-hook(注意' and " pylint的解析是严格的)

[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'

or

[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'

.. 而且

pylint --rcfile /path/to/pylintrc /path/to/module.py

这些都对我有用!

.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])'

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.

当我试图提交一个PR时,我得到了这个错误。我最终所做的只是在“import”发生的同一行上添加#pylint: disable=E0401。

这有助于我通过汽车考试。

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