[Python] Getting "Exception has occurred: AssertionError jinja2 must be installed to use...

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    First, I am new to Python and FastAPI. I created a little project for myself to learn creating several APIs, including using templates. I am working in a virtual environment using VS Code. Running pip freeze confirms that Jinja2 version 3.1.2 is installed in the virtual environment.

    (.venv) bash> pip freeze
    ..
    ..
    Jinja2==3.1.2enter code here
    ..


    I import Jinja2Templates in my main.py at line 15:

    from fastapi.templating import Jinja2Templates


    and reference this at line 54:

    templates = Jinja2Templates(directory="templates")


    This works fine when I start uvicorn from the command line in my virtual environment. In an effort to step through and debug my code, I added the following to main.py:

    if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)


    When I attempt to debug by clicking on VS Code's "Debug Python File"

    Debug Python File

    I run into the following exception:

    Exception has occurred: AssertionError
    jinja2 must be installed to use Jinja2Templates
    File "/Users/smb/Documents/VSCode/Python/DBInstances/main.py", line 54, in <module>
    templates = Jinja2Templates(directory="templates")


    Can anyone tell me what I'm missing and doing wrong?

    As requested in first comment, I am pasting in the launch.json contents.

    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "name": "Python: FastAPI",
    "type": "python",
    "request": "launch",
    "module": "uvicorn",
    "args": [
    "main:app",
    "--reload",
    "--port",
    "8000"
    ],
    "jinja": true,
    "justMyCode": true
    }
    ]
    }

    Continue reading...

Compartilhe esta Página