Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Pytest fails with "AttributeError: 'str' object has no attribute 'pattern'"

Tags:

python

pytest

Trying to import pytest on Python 3.8 fails with the following error:

>>> import pytest
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\pytest\__init__.py", line 6, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\_pytest\assertion\__init__.py", line 7, in <module>
    from _pytest.assertion import rewrite
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\_pytest\assertion\rewrite.py", line 24, in <module>
    from _pytest.assertion import util
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\_pytest\assertion\util.py", line 14, in <module>
    import _pytest._code
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\_pytest\_code\__init__.py", line 2, in <module>
    from .code import Code  # noqa
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\_pytest\_code\code.py", line 28, in <module>
    import pluggy
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\pluggy\__init__.py", line 16, in <module>
    from .manager import PluginManager, PluginValidationError
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\pluggy\manager.py", line 6, in <module>
    import importlib_metadata
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\importlib_metadata\__init__.py", line 478, in <module>
    __version__ = version(__name__)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\importlib_metadata\__init__.py", line 445, in version
    return distribution(package).version
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\importlib_metadata\__init__.py", line 418, in distribution
    return Distribution.from_name(package)
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\site-packages\importlib_metadata\__init__.py", line 179, in from_name
    dists = resolver(name)
  File "<frozen importlib._bootstrap_external>", line 1382, in find_distributions
  File "C:\Users\Al\AppData\Local\Programs\Python\Python38\lib\importlib\metadata.py", line 379, in find_distributions
    found = cls._search_paths(context.pattern, context.path)
AttributeError: 'str' object has no attribute 'pattern'

How do I fix this?

like image 540
Al Sweigart Avatar asked Jan 01 '20 00:01

Al Sweigart


1 Answers

This was fixed on November 11, 2019 in this commit.

The solution is to update the pluggy dependency (the error happens in version 0.12.0) by running pip install -U pluggy (or pip3 install -U pluggy on macOS and Linux). Simply updating pytest is not enough to update its dependency.

After running pip, you should be able to import pytest without error.

like image 68
Al Sweigart Avatar answered Sep 23 '22 14:09

Al Sweigart