Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Working outside of application context. #29

Open
kivanc57 opened this issue Nov 25, 2024 · 0 comments
Open

RuntimeError: Working outside of application context. #29

kivanc57 opened this issue Nov 25, 2024 · 0 comments

Comments

@kivanc57
Copy link

kivanc57 commented Nov 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant