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

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

其他回答

这些都对我有用!

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

你好,我能够从不同的目录导入包。我只是做了以下事情: 注意:我正在使用VScode

创建Python包的步骤 使用Python包非常简单。你所需要做的就是:

创建一个目录,并给它您的包的名称。 把你的课程放在里面。 在该目录中创建__init__.py文件

例如:你有一个名为Framework的文件夹,你在那里保存了所有的自定义类,你的工作就是在名为Framework的文件夹中创建一个__init__.py文件。

在导入时,你需要以这种方式导入——>

from Framework import base

因此E0401错误消失 Framework是你刚刚创建__init__.py和 Base是您需要导入并处理的自定义模块 希望能有所帮助!!!!

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

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

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

我刚刚发现的一个解决办法是,实际上只对整个包运行PyLint,而不是对单个文件运行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.