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

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

其他回答

我知道有两个选择。

第一,更改PYTHONPATH环境变量以包括模块上面的目录。

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

[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'

(或者在其他版本的pylint中,init-hook要求你将[General]更改为[MASTER])

这两种选择都应该有效。

在这两个目录中都有一个空的__init__.py文件来让python知道dirs是模块吗?

当你不是从文件夹中运行时(比如可能从pylint的文件夹中运行,尽管我没有用过),基本的大纲是:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

这就是python解释器如何在不引用当前目录的情况下感知模块,因此如果pylint从它自己的绝对路径运行,它将能够以topdir的身份访问functions_etc.py。Functions_etc或topdir.subdir。other_functions,前提是topdir在PYTHONPATH上。

UPDATE: If the problem is not the __init__.py file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages -- that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don't!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages - copying it over has the same effect.

只需将此代码添加到.vscode/settings。json文件

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

这将通知pylint的位置(这是python的错误检查器)

我不知道它是如何与WingIDE一起工作的,但是为了与Geany一起使用PyLint,我将我的外部命令设置为:

PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"

其中%f是文件名,%d是路径。可能对某些人有用:)

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

from .. import one

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