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

我怎么解决这个问题?


当前回答

安装Python时,可以设置路径。如果path已经定义了,那么你可以在VS Code中,按Ctrl+Shift+P,输入Python:选择解释器,然后选择Python的更新版本。点击此链接获取更多信息,https://code.visualstudio.com/docs/python/environments

其他回答

我找到了一个很好的答案。编辑pylintrc并在master中添加以下内容

init-hook="import imp, os; from pylint.config import find_pylintrc; imp.load_source('import_hook', os.path.join(os.path.dirname(find_pylintrc()), 'import_hook.py'))"

在这两个目录中都有一个空的__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.

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

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

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

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

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

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

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

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

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