问答 求教:pytest 安装后,查看 pytest --version 报错

Jason_chen · 2021年06月30日 · 最后由 Jason_chen 回复于 2021年06月30日 · 4804 次阅读

我使用以下命令 pip install -U pytest,安装了 pytest,显示是正常安装了,但是 pytest --version 查看版本后,出现了报错,我的 Python 是 3.7.0 版本的,最新 pytest 是 6.2.4 版本,怀疑是版本兼容问题,又使用了 5.3.0 和 4 的版本,依然没有解决,求教下大家,有没有什么解决方法

C:\Users\chenlibao>pip install -U pytest
Requirement already up-to-date: pytest in c:\python37\lib\site-packages (6.2.4)
Requirement already satisfied, skipping upgrade: atomicwrites>=1.0; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest) (1.4.0)
Requirement already satisfied, skipping upgrade: iniconfig in c:\python37\lib\site-packages (from pytest) (1.1.1)
Requirement already satisfied, skipping upgrade: packaging in c:\python37\lib\site-packages (from pytest) (20.9)
Requirement already satisfied, skipping upgrade: py>=1.8.2 in c:\python37\lib\site-packages (from pytest) (1.10.0)
Requirement already satisfied, skipping upgrade: toml in c:\python37\lib\site-packages (from pytest) (0.10.2)
Requirement already satisfied, skipping upgrade: attrs>=19.2.0 in c:\python37\lib\site-packages (from pytest) (19.2.0)
Requirement already satisfied, skipping upgrade: importlib-metadata>=0.12; python_version < "3.8" in c:\python37\lib\site-packages (from pytest) (1.3.0)
Requirement already satisfied, skipping upgrade: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest) (0.4.1)
Requirement already satisfied, skipping upgrade: pluggy<1.0.0a1,>=0.12 in c:\python37\lib\site-packages (from pytest) (0.13.1)
Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in c:\python37\lib\site-packages (from packaging->pytest) (2.4.2)
Requirement already satisfied, skipping upgrade: zipp>=0.5 in c:\python37\lib\site-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest) (0.6.0)
Requirement already satisfied, skipping upgrade: more-itertools in c:\python37\lib\site-packages (from zipp>=0.5->importlib-metadata>=0.12; python_version < "3.8"->pytest) (8.0.2)
You are using pip version 19.0.3, however version 21.1.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

查看版本,出现报错

C:\Users\chenlibao>pytest --version
Traceback (most recent call last):
  File "c:\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python37\Scripts\pytest.exe\__main__.py", line 9, in <module>
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 185, in console_main
    code = main()
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 143, in main
    config = _prepareconfig(args, plugins)
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 319, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "c:\python37\lib\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "c:\python37\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "c:\python37\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "c:\python37\lib\site-packages\pluggy\callers.py", line 203, in _multicall
    gen.send(outcome)
  File "c:\python37\lib\site-packages\_pytest\helpconfig.py", line 100, in pytest_cmdline_parse
    config: Config = outcome.get_result()
  File "c:\python37\lib\site-packages\pluggy\callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "c:\python37\lib\site-packages\pluggy\callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 1003, in pytest_cmdline_parse
    self.parse(args)
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 1283, in parse
    self._preparse(args, addopts=addopts)
  File "c:\python37\lib\site-packages\_pytest\config\__init__.py", line 1172, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "c:\python37\lib\site-packages\pluggy\manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
  File "c:\python37\lib\site-packages\importlib_metadata\__init__.py", line 94, in load
    module = import_module(match.group('module'))
  File "c:\python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\_pytest\assertion\rewrite.py", line 170, in exec_module
    exec(co, module.__dict__)
  File "c:\python37\lib\site-packages\celery\contrib\pytest.py", line 6, in <module>
    from .testing import worker
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\_pytest\assertion\rewrite.py", line 170, in exec_module
    exec(co, module.__dict__)
  File "c:\python37\lib\site-packages\celery\contrib\testing\worker.py", line 6, in <module>
    from celery import worker
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\_pytest\assertion\rewrite.py", line 170, in exec_module
    exec(co, module.__dict__)
  File "c:\python37\lib\site-packages\celery\worker\__init__.py", line 3, in <module>
    from .worker import WorkController
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\_pytest\assertion\rewrite.py", line 161, in exec_module
    source_stat, co = _rewrite_test(fn, self.config)
  File "c:\python37\lib\site-packages\_pytest\assertion\rewrite.py", line 354, in _rewrite_test
    tree = ast.parse(source, filename=fn_)
  File "c:\python37\lib\ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "c:\python37\lib\site-packages\celery\worker\worker.py", line 241
    self._conninfo.transport.implements.async and
                                            ^
SyntaxError: invalid syntax
共收到 5 条回复 时间 点赞


从这里看,你这是没有安装成功啊?先升级一下 pip 然后再安装

Dawson 回复

这是因为我之前已经安装好了,所以用这个命令,第一行就提示 Requirement already up-to-date: pytest in c:\python37\lib\site-packages (6.2.4),这里提示已经满足最新版本了;另外,pip 升级这个,是一直都有,不会影响,之前安装其他的包也会有这个提示。

我使用 import pytest 是可以成功的,但是使用命令行调用 pytest 就会有报错,包括 pytest --version

看着像是 celery 与 python 的版本冲突,网上有相关的详细说明,解决的话 python 降级或者 celery 降级。

MarvinWu 回复

感谢解答,问题解决了,我升级 celery 为最新版本(pip install -U celery),然后问题就解决了,我原先以为是 Python 与 pytest 版本兼容问题,没想到是与 celery 版本兼容的问题。

Dawson 回复

感谢解答~

Jason_chen 关闭了讨论 06月30日 21:47
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册