我在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'
我怎么解决这个问题?
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
关键是将你的项目目录添加到sys。路径而不考虑env变量。
对于使用VSCode的人,如果你的项目有一个基本目录,这里有一个简单的解决方案:
[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'
让我来解释一下:
re.search (r”。+\/" + base_dir, os.getcwd()).group():根据编辑文件查找基目录
Os.path.join (os.getcwd(), base_dir):添加CWD到系统。满足命令行环境的路径
供你参考,这是我的.pylintrc:
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f