Search code examples
pythonflaskvisual-studio-codevscode-debugger

Setup custom launcher with paths as a param values in VS Code (Flask - certificate path)


There is an example in Visual Studio Code python setup tutorial it is shown how to setup Flask. My problem starts when I try to provide self-signed certificate paths. I've tried to add following configuration:

    {
        "name": "Python: Flask",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "app.py"
        },
        "args": [
            "run",
            "--debugger",
            "--no-reload",
            "--host",
            "0.0.0.0",
            "--port",
            "4443",
            "--key",
            "${workspaceFolder}\\viberbot\\key.pem",
            "--cert",
            "${workspaceFolder}\\viberbot\\certificate.pem"
        ],
        "jinja": true
    },

I've tried to provide key/cert as:

  • C:\\FULL_PATH\\TO\\FILE
  • C:/FULL_PATH/TO/FILE
  • 'C:\\FULL_PATH\\TO\\FILE'
  • \"C:\\FULL_PATH\\TO\\FILE\"

and I always getting one of these error messages: Error: Invalid value for "--key": "--cert" must also be specified. or Error: Invalid value for "--key": File ""E:\Docs\learn_py\viberbot\key.pem"" does not exist.

This is the exact command generated by VS Code extension:

cd e:\Docs\learn_py && cmd /C "set "FLASK_APP=app.py" && set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe c:\Users\user.vscode\extensions\ms-python.python-2019.2.5558\pythonFiles\ptvsd_launcher.py --default --client --host localhost --port 50573 -m flask run --debugger --no-reload --host 0.0.0.0 --port 4443 --key E:\Docs\learn_py\viberbot\key.pem --cert E:\Docs\learn_py\viberbot\certificate.pem " Usage: python -m flask run [OPTIONS] Try "python -m flask run --help" for help.

Error: Invalid value for "--key": "--cert" must also be specified.

UPDATE: after making the change suggested by Brett I've started to get another error:

 * Serving Flask app "app.py"
 * Environment: development
 * Debug mode: off
Usage: python -m flask run [OPTIONS]

Error: Could not import "app".

I've tried to change app.py to ${workspaceFolder}\\viberbot\\app.py or providing a path like /E/path/viberbot/app.py and it didn't work.

UPDATE2: I needed to do "FLASK_APP": "viberbot\\app.py"


Solution

  • It might be because the example in the tutorial is out of date compared to the default that VS Code provides. Try:

    {
                "name": "Flask",
                "type": "python",
                "request": "launch",
                "module": "flask",
                "env": {
                    "FLASK_APP": "app.py",
                    "FLASK_ENV": "development",
                    "FLASK_DEBUG": "0"
                },
                "args": [
                    "run",
                    "--no-debugger",
                    "--no-reload",
                    "--cert",
                    "${workspaceFolder}\\viberbot\\certificate.pem",
                    "--key",
                    "${workspaceFolder}\\viberbot\\key.pem"
                ],
                "jinja": true
            }