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

其他回答

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.

我也有同样的问题,并通过在我的virtualenv中安装pylint来修复它,然后在文件中添加一个.pylintrc文件到我的项目目录:

[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'

如果使用vscode,请确保包目录不在_pychache__目录中。

关键是将你的项目目录添加到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

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