0

我有以下项目结构

- root
    - main.py
    - test/
        - __init__.py
        - conftest.py
        - test.py

conftest.py我有一些使用mysql.connector. 因此我正在导入:

import mysql.connector

@pytest.fixture(scope='module')  # maintain connection for all tests
def cnx(database, username, password):
    cnx = mysql.connector.connect(database=database, user=username, password=password)
    yield cnx
    cnx.close()

当我test.py使用以下命令从根目录中的终端运行时:

pytest tests/test.py

我收到以下错误:

ImportError while loading conftest '/tests/conftest.py'.
tests/conftest.py:5: in <module>
    import mysql.connector
E   ModuleNotFoundError: No module named 'mysql'

为什么会这样?mysql肯定是安装的。

4

2 回答 2

0

您是否尝试过在 CLI 上运行该 python?然后尝试是否import mysql.connector有效?另一个解决方案是卸载当前版本的 MySQL 并尝试使用这个包pip install mysql-connector-python-rf

于 2021-01-30T15:42:46.947 回答
-1

运行测试脚本的命令pytest不是pytest tests/test.py但是

python -m pytest tests/test.py

官方文档中所述。

于 2021-01-30T15:41:41.797 回答