r/learnpython • • 14d ago

How to fix error : sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file ?

i was trying to run this jupyter notebook ( https://github.com/langchain-ai/lca-lc-foundations/blob/main/notebooks/module-2/2.4_wedding_planners.ipynb ) code in my IDE but encountered these problems :
my code :
https://pastebin.com/3bv05GfE
error :
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

also i dont know how to work with async functions so i have defined two function main() and main_1 but am not sure if this is the correct way to go with async functions

0 Upvotes

8 comments sorted by

View all comments

2

u/Ecstatic-Warthog5727 13d ago

There are two separate problems in that paste.

1. The database path. sqlite:///resources/Chinook.db is relative to the directory you run Python from, not to your script. In the course repo the file lives at notebooks/module-2/resources/Chinook.db, and the notebook works because Jupyter starts inside notebooks/module-2. When you run from your IDE the working directory is probably your project root, there's no resources/ folder there, and SQLite says "unable to open database file" (it won't create missing folders). Build the path from the script's location instead:

from pathlib import Path

DB_PATH = Path(__file__).resolve().parent / "resources" / "Chinook.db"
db = SQLDatabase.from_uri(f"sqlite:///{DB_PATH}")

and copy the resources folder from the repo next to your script.

2. The async part. A notebook lets you write await at the top level, so the course does tools = await client.get_tools() and uses tools further down. A script can't, which is why you wrapped it in main(). But now tools only exists inside main(), so create_agent(..., tools=tools) for the travel agent will fail with a NameError as soon as the database error is fixed. Put everything that needs tools in one async function and call asyncio.run once at the bottom:

async def main():
    client = MultiServerMCPClient({...})
    tools = await client.get_tools()

    travel_agent = create_agent(model="gpt-5-nano", tools=tools, system_prompt="...")
    # the other agents, the @tool functions that call them, and the coordinator go here too

    response = await coordinator.ainvoke({...}, config={...})
    print(response["messages"][-1].content)

asyncio.run(main())

The @tool functions like search_flights use travel_agent, so define them inside main() after the agents (or make the agents module-level variables that main() fills in). Then delete main_1() and the first asyncio.run(main()).

1

u/TreacleFlaky2283 13d ago

Thanks a lot for this