简体   繁体   English

如何修复仅导入 pytest 时出现的属性错误

[英]How to fix Attribute Error in just importing pytest

I am importing pytest in a file and I am getting an AttributeError.我正在将 pytest 导入文件中,但出现 AttributeError。

The error is: AttributeError: 'WindowsPath' object has no attribute 'read_text'错误是: 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?有没有办法在我的机器上使用 pytest? I have a windows machine.我有一台 windows 机器。 Python version - 3.6.0 Pytest version - 5.2.4 Python 版本 - 3.6.0 Pytest 版本 - 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 .我在 Python2 中遇到的问题是pathlib是为 Python3 编写的模块,虽然有 Python 2.7 的移植,但移植 (1.0.1) 缺少 read_text()方法 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.在您的日志中,从字符串调用read_text() ,这是 joinpath() 的结果,而 read_text() 不是字符串的方法或属性。 That was the whole purpose of pathlib这就是pathlib的全部目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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