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

我怎么解决这个问题?


当前回答

把另一个可能的答案扔到堆里。我尝试了这里提供的许多解决方案。要么是我做错了,要么是我的pylint导入错误是由其他原因引起的。不管怎样,我的解决方法非常简单。

解决方案:重新安装包含您无法导入的所有模块的包(PyPi包而不是pip包),然后重新安装pylint,以便pylint和这些模块由相同的pip安装。

对我来说,拥有正确的目录并不管用。我从另一个关于他的问题的帖子中得到了这个想法。

其他回答

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

然后搜索settings.json

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

{

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

}

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.

如果你在Linux中使用Cython,我决定删除module.cpython-XXm-X-linux-gnu。我的项目目标目录下的文件。

我发现这在我的本地.pylintrc文件和pipenv虚拟环境中工作得很好:

[MASTER]
init-hook='import site; sys.path += site.getsitepackages()'

有关站点包的信息请参阅这篇文章。

我知道有两个选择。

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

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

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

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

这两种选择都应该有效。