[Python] Unable to fix ModuleNotFoundError in Flask despite proper convention

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    My Flask backend directory structure looks like this:

    backend/
    ├── app/
    │ ├── __init__.py
    │ ├── routes/
    │ │ ├── dev_routes.py
    │ │ └── ...
    │ └── models.py
    ├── tests/
    │ ├── __init__.py
    │ └── routes/
    │ └── test_dev_routes.py
    └── main.py
    |__ ...



    I'm trying to run my test_dev_routes.py file via the command pytest tests/routes when I'm in my backend folder in my terminal. I keep facing this error:

    _______ ERROR collecting tests/routes/test_dev_routes.py ________
    ImportError while importing test module '/Users/name/folder/project-name/project/backend/tests/routes/test_dev_routes.py'.
    Hint: make sure your test modules/packages have valid Python names.
    Traceback:
    ../../../../miniconda3/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    tests/routes/test_dev_routes.py:3: in <module>
    from app.routes import dev_routes
    E ModuleNotFoundError: No module named 'app'


    This is what the test_dev_routes.py file looks like:

    import pytest
    from flask import Flask, json
    from app.routes import dev_routes
    from app import create_app, db
    from app.models import User, Doctor, Slot, Queue, QueueEntry

    @pytest.fixture
    def client():
    app = Flask(__name__)
    app.register_blueprint(dev_routes.api, url_prefix='/api/dev')
    app.config['TESTING'] = True
    with app.test_client() as client:
    with app.app_context():
    db.create_all() # Create the database tables
    yield client
    with app.app_context():
    db.drop_all() # Clean up after tests


    My dev_routes.py file contains this api = Blueprint('dev_api', __name__) and it works properly when I retrieve this api variable from other files when I'm inside the app folder. I also tried moving my tests/routes folder into app and I still faced the same error. I don't understand what I'm doing wrong.

    The __init__.py file in my app folder looks like this:

    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    from flask_cors import CORS

    db = SQLAlchemy()
    _app_instance = None # Singleton instance

    def create_app(config_name: str = None):
    global _app_instance
    if _app_instance is None:
    _app_instance = Flask(__name__)
    _app_instance.config.from_object('config.Test')
    db.init_app(_app_instance)
    CORS(_app_instance)
    return _app_instance


    I tried setting the Pythonpath: export PYTHONPATH=$(pwd)/backend, and this didn't work.

    I tried moving my entire tests folder into my app folder and that still didn't work.

    I tried doing something like importing backend.app.routes wherein I put a __init__.py file in my backend folder, that still didn't work.

    Continue reading...

Compartilhe esta Página