Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

459
Visualizações
pytest with Flask can't locate module

I am learning Flask following this tutorial https://flask.palletsprojects.com/en/1.1.x/#user-s-guide but, when I try to run pytest, I get this error:

(FlaskVenv) MacBook-Pro-de-Hugo:FlaskProject hugovillalobos$ pytest
ImportError while loading conftest '/Users/hugovillalobos/Documents/Code/FlaskProject/tests/conftest.py'.
tests/conftest.py:4: in <module>
    from Flaskr import create_app
E   ModuleNotFoundError: No module named 'Flaskr'

This is the module that raises the error (conftest.py):

import os
import tempfile
import pytest
from Flaskr import create_app
from Flaskr.db import get_db, init_db

with open(os.path.join(os.path.dirname(__file__), 'data.sql'), 'rb') as f:
    _data_sql = f.read().decode('utf8')

@pytest.fixture
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({
        'TESTING': True,
        'DATABASE': db_path,
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)

@pytest.fixture
def client(app):
    return app.test_client()

@pytest.fixture
def runner(app):
    return app.test_cli_runner()

This is the code for such module as shown in the tutorial (the only difference is that my module is capitalized):

enter image description here

I though it might be a problem with the location of my files, but I am mapping the tutorial project layout correctly:

enter image description here

I don't know what I am missing.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You might need to run pytest as a module in python (which adds the current directory to sys.path). I remember having a similar situation and this resolved it for me:

python -m pytest -vv

Source: https://docs.pytest.org/en/latest/usage.html#calling-pytest-through-python-m-pytest

You can invoke testing through the Python interpreter from the command line:

python -m pytest [...] This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.path.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda