Fix ImportError: cannot import name 'auth_bp' from 'app.auth' in Flask
This error occurs when Flask cannot import a Blueprint because of a circular import or the Blueprint object name does not match what you are importing. Fix it by deferring the import, using an application factory pattern, or ensuring the Blueprint variable name in the module matches the import statement exactly.
Reading the Stack Trace
Here's what each line means:
- File "/app/app/__init__.py", line 5, in <module>: The app package's __init__.py imports the auth Blueprint at module level, triggering the circular dependency chain.
- File "/app/app/auth/__init__.py", line 3, in <module>: The auth module imports from app.models, which in turn imports db from the app package that hasn't finished initializing.
- ImportError: cannot import name 'auth_bp' from partially initialized module 'app.auth': Python detects the circular import: app.auth is only partially loaded because it re-entered app/__init__.py before completing.
Common Causes
1. Circular import between app and blueprint
The app __init__.py imports the Blueprint at the top level, and the Blueprint module imports db from app, creating a circular dependency.
# app/__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from app.auth import auth_bp # circular import
def create_app():
app = Flask(__name__)
db.init_app(app)
app.register_blueprint(auth_bp)
return app
2. Blueprint variable name mismatch
The import statement references a name that does not exist in the target module because the Blueprint was defined with a different variable name.
# app/auth/__init__.py
from flask import Blueprint
bp = Blueprint('auth', __name__) # named 'bp' not 'auth_bp'
# app/__init__.py
from app.auth import auth_bp # wrong name
3. Missing __init__.py in blueprint package
The blueprint directory lacks an __init__.py file, so Python cannot treat it as a package and the import fails.
# Directory structure missing __init__.py:
# app/auth/routes.py (exists)
# app/auth/__init__.py (missing!)
from app.auth import auth_bp # fails
The Fix
Move the Blueprint import inside the create_app factory function so it runs after db and other extensions are fully initialized. This breaks the circular import chain because app/__init__.py finishes defining db before auth tries to import it.
# app/__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from app.auth import auth_bp # top-level import causes circular dependency
def create_app():
app = Flask(__name__)
db.init_app(app)
app.register_blueprint(auth_bp)
return app
# app/__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
db.init_app(app)
from app.auth import auth_bp # deferred import inside factory
app.register_blueprint(auth_bp)
return app
Testing the Fix
import pytest
from app import create_app
@pytest.fixture
def app():
app = create_app()
app.config['TESTING'] = True
return app
def test_app_creates_without_import_error(app):
assert app is not None
def test_auth_blueprint_registered(app):
assert 'auth' in [bp.name for bp in app.iter_blueprints()]
def test_auth_routes_accessible(app):
client = app.test_client()
response = client.get('/auth/login')
assert response.status_code in (200, 302)
Run your tests:
pytest tests/ -v
Pushing Through CI/CD
git checkout -b fix/flask-blueprint-circular-import,git add app/__init__.py,git commit -m "fix: defer blueprint import to break circular dependency",git push origin fix/flask-blueprint-circular-import
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- run: pytest tests/ -v --tb=short
- run: flake8 app/
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
pip install bugstack
Step 2: Initialize
import bugstack
bugstack.init(api_key=os.environ["BUGSTACK_API_KEY"])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run pytest locally to confirm the blueprint imports correctly.
- Open a pull request with the deferred import change.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify the app starts cleanly in staging.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other modules are affected by the import change before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
No. The import runs once during app creation. Python caches imported modules so subsequent references are instant.
Yes. The factory pattern is the recommended Flask approach for any non-trivial application. It eliminates circular imports and makes testing much easier.