Skip to content

RuntimeError: Working outside of application context. #29

@kivanc57

Description

@kivanc57

Whenever I try to set the Python driver as it's shown below. I follow the instructions that are shown in this link from the Python course. So it is pretty much like:

import os
from flask import Flask, current_app
from neo4j import GraphDatabase

NEO4J_URI=os.getenv('NEO4J_URI')
NEO4J_USERNAME=os.getenv('NEO4J_USERNAME')
NEO4J_PASSWORD=os.getenv('NEO4J_PASSWORD')

"""
Initiate the Neo4j Driver
"""
def init_driver(uri, username, password):
    # Create an instance of the driver
    current_app.driver = GraphDatabase.driver(uri, auth=(username, password))

    # Verify Connectivity
    current_app.driver.verify_connectivity()

    return current_app.driver

And this is the error when I testi it or execute it:

(venv) kivanc57@linuxmint:~/Desktop/neo4j_api$ pytest tests/01_connect_to_neo4j__test.py
ImportError while loading conftest '/home/kivanc57/Desktop/neo4j_api/tests/conftest.py'.
tests/conftest.py:7: in <module>
    from api import create_app
api/__init__.py:16: in <module>
    from .neo4j import init_driver
api/neo4j.py:44: in <module>
    init_driver(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD)
api/neo4j.py:15: in init_driver
    current_app.driver = GraphDatabase.driver(uri, auth=(username, password))
venv/lib/python3.12/site-packages/werkzeug/local.py:316: in __get__
    obj = instance._get_current_object()  # type: ignore[misc]
venv/lib/python3.12/site-packages/werkzeug/local.py:513: in _get_current_object
    raise RuntimeError(unbound_message) from None
E   RuntimeError: Working outside of application context.
E
E   This typically means that you attempted to use functionality that needed
E   the current application. To solve this, set up an application context
E   with app.app_context(). See the documentation for more information.

I know that it is because of wrong applications of Flask but then the course's code is problematic if I really have to change the Flask code whereas it should stay constant so I do not want to change it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions