简体   繁体   中英

How to fix Attribute Error in just importing pytest

I am importing pytest in a file and I am getting an AttributeError.

The error is: AttributeError: 'WindowsPath' object has no attribute 'read_text'

>>> import pytest
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\pytest.py", line 6, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\_pytest\assertion\__init__.py", line 7, in <module>
    from _pytest.assertion import rewrite
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\_pytest\assertion\rewrite.py", line 26, in <module>
    from _pytest.assertion import util
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\_pytest\assertion\util.py", line 8, in <module>
    import _pytest._code
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\_pytest\_code\__init__.py", line 2, in <module>
    from .code import Code  # noqa
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\_pytest\_code\code.py", line 24, in <module>
    import pluggy
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\pluggy\__init__.py", line 16, in <module>
    from .manager import PluginManager, PluginValidationError
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\pluggy\manager.py", line 11, in <module>
    import importlib_metadata
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\importlib_metadata\__init__.py", line 547, in <module>
    __version__ = version(__name__)
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\importlib_metadata\__init__.py", line 509, in version
    return distribution(distribution_name).version
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\importlib_metadata\__init__.py", line 260, in version
    return self.metadata['Version']
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\importlib_metadata\__init__.py", line 248, in metadata
    self.read_text('METADATA')
  File "--\AppData\Local\Programs\Python\Python36\Lib\site-packages\importlib_metadata\__init__.py", line 469, in read_text
    return self._path.joinpath(filename).read_text(encoding='utf-8')
AttributeError: 'WindowsPath' object has no attribute 'read_text'

Is there a way how I can use pytest on my machine? I have a windows machine. Python version - 3.6.0 Pytest version - 5.2.4

Thanks

The issue I run into in Python2 was that pathlib is a module that was written for Python3 and althought there is a porting for Python 2.7, the porting (1.0.1) is missing the read_text() method . In your log read_text() is being called from a string, that is the result of the joinpath(), and read_text() is not a method or attribute of a string. That was the whole purpose of pathlib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM