Error del módulo: no se encontró el módulo llamado (*)
Estoy intentando ejecutar mis pruebas utilizando python -m pytest
pero obtengo un error que dice ModuleNotFoundError: No module named 'sample'
. Cuando utilizo nosetests
o cualquier otra opción funciona bien, pero al intentar utilizar pytest, no funciona. Mi estructura de directorios se ve así abajo, ¿tienes algún consejo sobre por qué no funciona?
├── LICENSE.txt
├── README.md
├── data
│ └── data_file
├── exported_register.csv
├── pyproject.toml
├── requirements.txt
├── setup.cfg
├── setup.py
├── src
│ └── sample
│ ├── init.py
│ ├── pycache
│ │ ├── init.cpython-39.pyc
│ │ ├── dziennik.cpython-39.pyc
│ │ ├── przedmiot.cpython-39.pyc
│ │ ├── simple.cpython-39.pyc
│ │ └── uczen.cpython-39.pyc
│ ├── dziennik.py
│ ├── package_data.dat
│ ├── przedmiot.py
│ ├── simple.py
│ └── uczen.py
├── tests
│ ├── init.py
│ ├── pycache
│ │ ├── init.cpython-39.pyc
│ │ ├── test_ASSERTPY_uczen.cpython-39-pytest-6.2.1.pyc
│ │ ├── test_ASSERTPY_uczen.cpython-39-pytest-6.2.5.pyc
│ │ ├── test_ASSERTPY_uczen.cpython-39.pyc
│ │ ├── test_PYHAMCREST_uczen.cpython-39-pytest-6.2.1.pyc
│ │ ├── test_PYHAMCREST_uczen.cpython-39-pytest-6.2.5.pyc
│ │ ├── test_PYHAMCREST_uczen.cpython-39.pyc
│ │ ├── test_UNITTEST_register.cpython-39-pytest-6.2.1.pyc
│ │ ├── test_UNITTEST_register.cpython-39-pytest-6.2.5.pyc
│ │ ├── test_UNITTEST_register.cpython-39.pyc
│ │ ├── test_UNITTEST_uczen.cpython-39-pytest-6.2.1.pyc
│ │ ├── test_UNITTEST_uczen.cpython-39-pytest-6.2.5.pyc
│ │ ├── test_UNITTEST_uczen.cpython-39.pyc
│ │ ├── test_simple.cpython-39-pytest-6.2.1.pyc
│ │ ├── test_simple.cpython-39-pytest-6.2.5.pyc
│ │ └── test_simple.cpython-39.pyc
│ ├── test_ASSERTPY_uczen.py
│ ├── test_PYHAMCREST_uczen.py
│ ├── test_UNITTEST_register.py
│ ├── test_UNITTEST_uczen.py
│ └── test_simple.py
└── tox.ini
davy.ai
El error que está viendo (
ModuleNotFoundError: No module named 'sample'
) indica que pytest no puede localizar el módulosample
. Esto es probablemente debido al hecho de que pytest ejecuta las pruebas en un proceso separado, lo que puede causar problemas con ciertos cargadores de módulos.Para solucionar este problema, puede intentar agregar el directorio
src
al camino de Python en su archivo de prueba. Por ejemplo, puede agregar las siguientes líneas al comienzo de su archivotests/test_simple.py
:Esto agrega el directorio
src
al camino de Python, permitiendo a pytest localizar el módulosample
.Alternativamente, puede modificar la variable de entorno
PYTHONPATH
para incluir el directoriosrc
. Por ejemplo, puede agregar la siguiente línea a su archivo de configuración de shell (por ejemplo,.bashrc
o.zshrc
):Esto agregará el directorio
src
al camino de Python cada vez que abra una nueva terminal.Al agregar el directorio
src
al camino de Python, debería poder ejecutar pytest sin encontrar el problema deModuleNotFoundError
.