【问题标题】:Pylint: "Problem importing module ....: cannot import name 'Type'"Pylint:“导入模块时出现问题......:无法导入名称‘类型’”
【发布时间】:2018-12-07 16:40:35
【问题描述】:

每次运行 pylint(无论检查什么程序)都会产生以下错误消息:

(env) $ pylint hello.py 
Problem importing module logging.py: cannot import name 'Type'
Problem importing module spelling.py: cannot import name 'Type'
Problem importing module python3.py: cannot import name 'Type'
Problem importing module typecheck.py: cannot import name 'Type'
Problem importing module variables.py: cannot import name 'Type'
Problem importing module refactoring.py: cannot import name 'Type'
Problem importing module format.py: cannot import name 'Type'
Problem importing module imports.py: cannot import name 'Type'
Problem importing module utils.py: cannot import name 'Type'
Problem importing module newstyle.py: cannot import name 'Type'
Problem importing module exceptions.py: cannot import name 'Type'
Problem importing module classes.py: cannot import name 'Type'
Problem importing module stdlib.py: cannot import name 'Type'
Problem importing module async.py: cannot import name 'Type'
Problem importing module design_analysis.py: cannot import name 'Type'
Problem importing module base.py: cannot import name 'Type'
Problem importing module strings.py: cannot import name 'Type'
Traceback (most recent call last):
  File "/Users/brunorijsman/python-logging-performance/env/bin/pylint", line 11, in <module>
    sys.exit(run_pylint())
  File "/Users/brunorijsman/python-logging-performance/env/lib/python3.5/site-packages/pylint/__init__.py", line 20, in run_pylint
    Run(sys.argv[1:])
  File "/Users/brunorijsman/python-logging-performance/env/lib/python3.5/site-packages/pylint/lint.py", line 1557, in __init__
    linter.enable("c-extension-no-member")
  File "/Users/brunorijsman/python-logging-performance/env/lib/python3.5/site-packages/pylint/utils.py", line 323, in enable
    msgid, enable=True, scope=scope, line=line, ignore_unknown=ignore_unknown
  File "/Users/brunorijsman/python-logging-performance/env/lib/python3.5/site-packages/pylint/utils.py", line 366, in _set_msg_status
    msg = self.msgs_store.get_message_definition(msgid)
  File "/Users/brunorijsman/python-logging-performance/env/lib/python3.5/site-packages/pylint/utils.py", line 989, in get_message_definition
    msgid_or_symbol=msgid_or_symbol
pylint.exceptions.UnknownMessageError: No such message id c-extension-no-member

在这种特殊情况下,我正在检查以下琐碎的程序hello.py

print("Hello")

但是当我不检查程序时也会出现问题,例如pylint --version

Pylint 在我创建的虚拟环境中运行,如下所示:

$ python3 -m virtualenv env
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.5'
New python executable in /Users/brunorijsman/python-logging-performance/env/bin/python3
Also creating executable in /Users/brunorijsman/python-logging-performance/env/bin/python
Installing setuptools, pip, wheel...done.
$ source env/bin/activate
(env) $

(注意:我意识到这些天建议使用模块 venv 而不是 virtualenv,但我在 macOS 上遇到了不同的问题,如 'pip install' fails for every package ("Could not find a version that satisfies the requirement") 中所述)

我将pylint安装到虚拟环境中如下:

(env) $ pip install pylint
Collecting pylint
  Using cached https://files.pythonhosted.org/packages/a5/06/ecef826f319055e6b231716730d7f9047dd7524ffda224b521d989f085b6/pylint-2.2.2-py3-none-any.whl
Collecting isort>=4.2.5 (from pylint)
  Using cached https://files.pythonhosted.org/packages/1f/2c/22eee714d7199ae0464beda6ad5fedec8fee6a2f7ffd1e8f1840928fe318/isort-4.3.4-py3-none-any.whl
Collecting astroid>=2.0.0 (from pylint)
  Using cached https://files.pythonhosted.org/packages/fc/53/8809bc008bad0300897281a7b320b286dc0e84e836396c0cff6279841e8a/astroid-2.1.0-py3-none-any.whl
Collecting mccabe (from pylint)
  Using cached https://files.pythonhosted.org/packages/87/89/479dc97e18549e21354893e4ee4ef36db1d237534982482c3681ee6e7b57/mccabe-0.6.1-py2.py3-none-any.whl
Collecting typed-ast; python_version < "3.7" and implementation_name == "cpython" (from astroid>=2.0.0->pylint)
Collecting wrapt (from astroid>=2.0.0->pylint)
Collecting six (from astroid>=2.0.0->pylint)
  Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting lazy-object-proxy (from astroid>=2.0.0->pylint)
Installing collected packages: isort, typed-ast, wrapt, six, lazy-object-proxy, astroid, mccabe, pylint
Successfully installed astroid-2.1.0 isort-4.3.4 lazy-object-proxy-1.3.1 mccabe-0.6.1 pylint-2.2.2 six-1.11.0 typed-ast-1.1.0 wrapt-1.10.11

版本信息:

操作系统为 macOS High Sierra 10.13.6

(env) $ python --version
Python 3.5.1

【问题讨论】:

  • 看看这个可能会有所帮助:github.com/python/mypy/issues/1838
  • 试试这个pip install --pre -U pylint astroid或者这个pip install astroid==1.5.3 pylint==1.8.2
  • @i_th 后者(“pip install astroid==1.5.3 pylint==1.8.2”)确实解决了我的问题。但前者(“pip install --pre -U pylint astroid”)没有。如果您将其发布为答案,我将奖励赏金。您的第一条评论确实似乎描述了根本原因。如果您在答案中包含解释,则奖励业力。

标签: python python-3.x macos pip virtualenv


【解决方案1】:

您的 python 版本似乎与特定的 pylint 和 astroid 兼容。 Python 3.5 自带了自己的类型化版本,它是在引入 typing.Type 之前添加的。所以,这可能会解决问题:

pip install astroid==1.5.3 pylint==1.8.2

python 3.5 出现这个问题

https://github.com/PyCQA/pylint/issues/1216

https://github.com/python/mypy/issues/1838

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2013-02-22
    相关资源
    最近更新 更多