【问题标题】:pylint fails with "unable to import moto", python can import itpylint 因“无法导入 moto”而失败,python 可以导入它
【发布时间】:2019-11-25 06:13:11
【问题描述】:

我正在尝试 pylint 我的测试套件,该套件使用 moto 模块进行 AWS API 模型。

这是我超级简单的测试用例:

import moto

不幸的是它失败了pylint:

~/prj $ pylint test_moto.py 
************* Module test_test
test_moto.py:1:0: E0401: Unable to import 'moto' (import-error)
test_moto.py:1:0: W0611: Unused import moto (unused-import)

----------------------------------------------------------------------
Your code has been rated at -50.00/10 (previous run: -50.00/10, +0.00)

但是我的 virtualenv 中确实安装了 moto 并且它可以工作:

~/prj $ python
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import moto
>>> moto.__version__
'1.3.14'
>>> 

这是我的pylint:

~/prj $ pylint --version
pylint 2.3.1
astroid 2.2.5
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0]

python 和 pylint 都在 virtualenv 中:

~/prj $ which python
/home/me/.virtualenvs/prjenv/bin/python
~/prj $ which pylint
/home/me/.virtualenvs/prjenv/bin/pylint

为什么 pylint 抱怨进口?我怎样才能阻止它?

【问题讨论】:

  • 运行你用来运行代码的python安装的pylint。如果两者都在不同的地方(例如,一个在 venv 中,另一个在全局系统组件中),那么事情可能会出错。
  • @Jens 感谢您的建议,他们都在虚拟环境中。我已经更新了问题。
  • 您是否有文件moto.py 可能会影响模块?
  • @Jens 不,只有test_moto.py 和main.py。

标签: python pylint moto


【解决方案1】:

事实证明,pylint 2.3.x 的问题与boto3 和moto 的关系不佳。见https://github.com/PyCQA/pylint/issues/3261

降级到 pylint 2.2.x 可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2016-03-17
    • 2015-06-22
    • 2021-01-18
    相关资源
    最近更新 更多