0

I'm trying to do a basic thing: debug a python function app using a virtual environment.

The Visual Studio code documentation does not indicate how to do and I cannot figure it out myself: https://code.visualstudio.com/docs/python/debugging

I run the app from the terminal with the following command:

.venv36\scripts\activate
func start

The app is run from my virtual environment and everything is OK.

To debug, the debugger uses the launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Python Functions",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "windows": {
                "pythonPath": ".venv36\\Scripts\\python.exe"
            },
            "preLaunchTask": "func: host start"
        }
    ]
}

Reading the https://code.visualstudio.com/docs/python/environments, I added the following settings to the settings.json:

{
   ...
    "python.pythonPath": ".venv36\\Scripts\\python.exe",
    "python.pipenvPath": ".venv36\\Scripts\\pip.exe",
    "python.venvPath": ".venv36",
    "python.terminal.activateEnvironment": true,

The tasks.json is:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "command": "host start",
            "problemMatcher": "$func-watch",
            "isBackground": true,
            "options": {
                "cwd": "${workspaceFolder}/AlarmScoringFuncApp"
            }
        }
    ]
}

When I start debugging, the app is started but not in the right environment: it is using the system libraries instead of the ones installed for my project in the requirements.txt.

My app crash when it tries to use a library that is not installed in the system environment.

Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58
  • My experience is that if you create a virtual environment named only `.env` in the root folder of the project it just works. – arghol Nov 08 '19 at 19:39
  • According to my research, we can debug python function on local with vs code. For more details, please refer to https://learn.microsoft.com/en-us/azure/python/tutorial-vs-code-serverless-python-04 – Jim Xu Nov 11 '19 at 02:55
  • That is why I precised in the title 'using a virtual envrionment'. Each project has his own dependencies and his own environment, and Microsoft's documentation does not consider that. – Anthony Brenelière Nov 12 '19 at 16:03
  • 1
    FYI `"python.pipenvPath"` is for pipenv, not pip. Also, that setting plus `"python.venvPath"` are unnecessary for specifying your virtual environment; `"python.pythonPath"` is enough. – Brett Cannon Nov 13 '19 at 00:46

0 Answers0