pytest assume cannot be imported: solve the problem of ImportError: cannot import name 'assume' from 'pytest'

code show as below:

from pytest import assume

报错:
ImportError: cannot import name ‘assume’ from ‘pytest’ (C:\Users\zeng\AppData\Local\Programs\Python\Python39\lib\site-packages\pytest_init_.py)

First, I copied this error message from Baidu. Baidu has two opinions. One is that my .py file and the python built-in module have the same file name, so the ide cannot distinguish and the import fails. But after checking, there is nothing wrong with my .py file name.
Another way of saying is that the version of pytest I installed does not match the version of pytest I am using. Through the pip list, the module version is queried:
insert image description here
but the __init__.py of pytest does not find the version information, and the __init__.py of pytest_assume is opened, and the version does not match, so the version is changed to 2.4.2, but after the test, it is found , the problem was not resolved.

So according to the error message, open the C:\Users\zeng\AppData\Local\Programs\Python\Python39\lib\site-packages\pytest_init_.py file , and found that there are some import operations in this file. After searching, I found that Indeed assume is not imported.

Later, I found the pytest_assume module in the C:\Users\zeng\AppData\Local\Programs\Python\Python39\lib\site-packages directory. After searching, I found that the assume module exists in plugin.py in pytest_assume, so I
insert image description here
returned
insert image description here
to C:\Users\zeng\AppData\Local\Programs\Python\Python39\lib\site-packages\pytest_ init _.py This file, add a line of code inside:

from pytest_assume.plugin import assume

insert image description here
After saving, run the original python file using assume again, and the operation is successful. But a warning will be reported, which does not affect the use:
insert image description here

It is worth mentioning that after I initially pip install pytest-assume, I can succeed directly from pytest import assume; but after a few days, importing assume again will not work. This should also be the key to the warning. I am a little confused. I hope someone can give me some pointers on what is the reason.

Supplement:
It is used on PyCharm today, and it has been marked red, but it does not affect the use.
insert image description here
Replace it with

from pytest_assume.plugin import assume

after normal

Guess you like

Origin blog.csdn.net/tianshuiyimo/article/details/116519264