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

我怎么解决这个问题?


当前回答

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

from .. import one

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

其他回答

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

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.

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

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

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

如果你使用的是Windows:

获取刚刚创建的虚拟环境中python.exe的路径 它应该像这样Z:\YourProjectFolder\Scripts\python.exe 然后转到vscode,编辑user settings.json 添加这一行:"python。pythonPath环境”:“Z: \ \ YourProjectFolder \ \ \ \ python.exe脚本” 保存它,这应该可以解决问题 注意:在json文件中使用双反斜杠而不是单反斜杠

{
    "python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
}

我知道有两个选择。

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

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

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

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

这两种选择都应该有效。