【发布时间】:2020-11-04 01:33:55
【问题描述】:
我的 Python Chalice 项目运行良好,我的所有测试都通过了,但 Pylint 报告了 unresolved import 错误。
我已经在具有以下项目结构的 vanilla Python 项目中重现了该错误:
myproject
.vscode
settings.json
/api
/domain
__init__.py
weekday.py
__init.py
app.py
/tests
/api
/domain
__init__.py
test_weekday.py
__init__.py
__init__.py
.vscode/settings.json:
{
"python.pythonPath": "C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\python.exe"
}
weekday.py:
from enum import Enum
class Weekday(Enum):
MONDAY = 'mon'
TUESDAY = 'tue'
WEDNESDAY = 'wed'
THURSDAY = 'thu'
FRIDAY = 'fri'
class InvalidValue(Exception):
pass
def parse(key: str) -> Weekday:
try:
return Weekday(key)
except Exception as e:
raise InvalidValue() from e
app.py:
from domain.weekday import Weekday
if __name__ == '__main__':
print(Weekday.MONDAY.value)
print('done!')
test_weekday.py:
from unittest import TestCase
from api.domain import weekday
from api.domain.weekday import Weekday
class StateTests(TestCase):
def test_validParsing(self):
self.assertEqual(weekday.parse('mon'), Weekday.MONDAY)
self.assertEqual(weekday.parse('tue'), Weekday.TUESDAY)
self.assertEqual(weekday.parse('wed'), Weekday.WEDNESDAY)
self.assertEqual(weekday.parse('thu'), Weekday.THURSDAY)
self.assertEqual(weekday.parse('fri'), Weekday.FRIDAY)
def test_invalidParsing(self):
with self.assertRaises(weekday.InvalidValue):
weekday.parse('invalid value')
以下是我的 Python 特定 VS Code 设置:
"python.languageServer": "Microsoft",
"python.linting.pylintEnabled": true,
"python.linting.pylintUseMinimalCheckers": false,
"python.linting.pylintArgs": [
"--disable=C0111"
],
"python.pythonPath": "C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",
"python.linting.mypyCategorySeverity.note": "Warning",
"python.linting.mypyEnabled": true
我用python api/app.py 运行我的普通Python 应用程序,用python -m unittest -f 运行我的测试。
在我上面的普通 Python 项目中,我收到以下错误和警告(测试没有报告任何问题,这很奇怪):
app.py
ERROR: Unable to import 'domain.weekday' pylint(import-error)
WARNING: unresolved import 'domain.weekday' Python(unresolved-import)
在我的 Python Chalice 项目中,我收到以下错误和警告:
app.py
ERROR: Unable to import 'domain' pylint(import-error)
WARNING: unresolved import 'domain' Python (unresolved-import)
test_weekday.py
ERROR: Unable to import 'api.domain' pylint(import-error)
ERROR: Unable to import 'api.domain.weekday' pylint(import-error)
同样,两个应用都运行良好,我的所有测试都通过了。
我尝试将以下内容添加到我的.vscode/settings.json,但没有帮助:
{
"python.autoComplete.extraPaths": [
"./api",
"./tests"
],
}
如何配置 Pylint 以在此项目结构中正常工作?
【问题讨论】:
标签: python visual-studio-code warnings pylint