In tox testenv "ModuleNotFoundError: No module named 'pandas" error comes whenever pandas is added to deps

In tox testenv "ModuleNotFoundError: No module named 'pandas" error comes whenever pandas is added to deps

在我使用 pandas 实施测试后,我的构建失败并显示“ModuleNotFoundError:没有名为 'pandas' 的模块”错误,但是,我将 pandas 添加到testenv deps 在日志文件中我也看到它已安装。在 boto3 的情况下我得到了同样的错误但是在我将它添加到 deps 之后,它解决了问题但是在 pandas 的情况下它不起作用。

tox.ini

[tox]         
envlist=flake8
        py36_tests

[testenv]
deps=pytest
     flake8

[testenv:py36_tests]
basepython=python3.6
deps=boto3
     pandas
commands=py.test -s -v tests --junitxml=report.xml

[testenv:flake8]
commands=flake8 --exclude=.git,__pycache__,__init__.py data_collector/
         flake8 tests/
         flake8 setup.py
         flake8 setup-cy.py
         flake8 Docker/startup_scripts/
         flake8 bin/data_collector

日志:

$ tox -e py36_tests
GLOB sdist-make: /builds/<path>/<my_package>/setup.py
py36_tests create: /builds/<path>/<my_package>/.tox/py36_tests
py36_tests installdeps: boto3, pandas
py36_tests inst: /builds/<path>/.tox/.tmp/package/1/<my_package>-0.0.0.zip
py36_tests installed: <many packages>, pandas==1.1.1, <many packages>
y36_tests run-test-pre: PYTHONHASHSEED='3745093701'
py36_tests run-test: commands[0] | py.test -s -v tests --junitxml=report.xml
WARNING: test command found but not installed in testenv
  cmd: /usr/local/bin/py.test
  env: /builds/<path>/<my_package>/.tox/py36_tests
Maybe you forgot to specify a dependency? See also the allowlist_externals envconfig setting.
DEPRECATION WARNING: this will be an error in tox 4 and above!
============================= test session starts ==============================
platform linux -- Python 3.6.15, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /usr/bin/python3.6
cachedir: .tox/py36_tests/.pytest_cache
rootdir: /builds/<path>/<my_package>
collecting ... collected 10 items / 1 error / 9 selected
==================================== ERRORS ====================================
________________ ERROR collecting tests/ingestion/test_task.py _________________
ImportError while importing test module '/builds/<path>/tests/ingestion/test_task.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.6/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/ingestion/test_task.py:3: in <module>
    import pandas as pd
E   ModuleNotFoundError: No module named 'pandas'
- generated xml file: /builds/<path>/<my_package>/report.xml -
=========================== short test summary info ============================
ERROR tests/ingestion/test_task.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.24s ===============================
ERROR: InvocationError for command /usr/local/bin/py.test -s -v tests --junitxml=report.xml (exited with code 2)
___________________________________ summary ____________________________________
ERROR:   py36_tests: commands failed

虽然我没有在我的测试中导入任何东西,但它运行良好。有什么问题?

看到这个

WARNING: test command found but not installed in testenv
  cmd: /usr/local/bin/py.test

您还没有将 py.test 安装到虚拟环境中,所以 tox 找到了一个全局的。当然,全局的运行在全局 Python 中,并且对你在 tox 中的虚拟环境一无所知。

要修复它,请将 pytest 安装到虚拟环境中:

[testenv:py36_tests]
deps=boto3
     pandas
     pytest

我在 tox 调用后使用了一个虚拟环境,解决方案是在虚拟环境已经激活时移动 tox 调用。