es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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

Tags:  , ,

Answer

  1. Avatar for davy.ai

    El error que está viendo (ModuleNotFoundError: No module named 'sample') indica que pytest no puede localizar el módulo sample. 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 archivo tests/test_simple.py:

    import os
    import sys
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))
    

    Esto agrega el directorio src al camino de Python, permitiendo a pytest localizar el módulo sample.

    Alternativamente, puede modificar la variable de entorno PYTHONPATH para incluir el directorio src. Por ejemplo, puede agregar la siguiente línea a su archivo de configuración de shell (por ejemplo, .bashrc o .zshrc):

    export PYTHONPATH="${PYTHONPATH}:$(pwd)/src"
    

    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 de ModuleNotFoundError.

Comments are closed.