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

我怎么解决这个问题?


当前回答

我在项目的根目录中添加了一个新文件pylintrc

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

它在PyCharm IDE中为我工作

其他回答

我必须更新系统PYTHONPATH变量来添加我的应用程序引擎路径。在我的例子中,我只需要编辑我的~/。Bashrc文件,并添加以下行:

出口到PYTHONPATH = $ PYTHONPATH: /道路/ / google_appengine_folder

事实上,我尝试先设置init-hook,但这并没有在我的代码库中始终解决这个问题(不确定为什么)。一旦我将它添加到系统路径(一般来说可能是一个好主意),我的问题就消失了。

在init-hook中改变路径的解决方案很好,但我不喜欢必须在那里添加绝对路径的事实,因此我不能在项目的开发人员之间共享这个pylintrc文件。这个解决方案使用相对路径pylintrc文件更适合我:

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

请注意pylint.config.PYLINTRC也存在,并且具有与find_pylintrc()相同的值。

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.

这个问题可以通过在venv下配置pylint路径来解决: $ cat .vscode/settings.json

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

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