0

I've a Python project in Windows which provides a command line application via console_script. The Python package uses a poetry virtualenv in <project-root>/.venv and is installed into the venv in editable mode.

Now I'd like to debug the command line application in the integrated VSCode terminal. To beeing able to do so I created a launch.json config like follows:

{
    // 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: Device Agent (WLAN)",
            "type": "python",
            "request": "launch",
            "program": "mvp-end-device-agent",
            "args": ["<", "-r", "D:\\MassHunter\\Data\\demo_0000.d"],
            "console": "integratedTerminal"
        }
    ]
}

If I start the debugger a new terminal is opened and I get the following error:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.


PS D:\mvp-end-device-agent>  & 'd:\mvp-end-device-agent\.venv\Scripts\python.exe' 'c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '58875' '--' 'mvp-end-device-agent' '<' '-r' 'D:\MassHunter\Data\demo_0000.d'
Traceback (most recent call last):
  File "d:\python\python386\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\python\python386\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "d:\python\python386\lib\runpy.py", line 264, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "d:\python\python386\lib\runpy.py", line 234, in _get_code_from_file
    with io.open_code(decoded_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\mvp-end-device-agent\\mvp-end-device-agent'
PS D:\mvp-end-device-agent> & d:/mvp-end-device-agent/.venv/Scripts/Activate.ps1
(.venv) PS D:\mvp-end-device-agent>

It seems like the venv is not activated before executing the command line application but afterwards. According to VSCode docs - Where the extension looks for environments and microsoft/vscode-python/issues/8372 poetry venvs are hardly supported ATM.

I already tried to specify the venv Python environment manually by adding

{
    "python.pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe"
}

to the project local .vscode/settings.json file without success.

How can I fix or workaround this?

3
  • @panagiotis-kanavos Do you think that's a bug in VSCode? Commented Oct 20, 2020 at 9:32
  • Have you tried activating the venv manually like this: stackoverflow.com/questions/64439019/…, then execute the project by python filename.py? Commented Oct 28, 2020 at 9:20
  • The activation of the venv is no issue. It works automatically as well as manually. However if you run the debugger a new terminal instance is created and the venv needs to be activated as part of the debugger + venv manager integration. Commented Oct 28, 2020 at 9:34

2 Answers 2

1

That's not a bug. The default Terminal prefix should be PS \the current working directory\, so when we start debugging, it will open a new terminal instance which shows the default prefix. However when we look at its execution scripts:

enter image description here

The first one is the .venv\python.exe which we select it as interpreter, after debugging finished, the environment was activated. Then you choose debug again, this terminal will keep .venv being activated.

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is w.r.t. a Python module. I have a console_script however. The issue was that I mis-interpreted "program" to be suitable for executables as well and is solved now: stackoverflow.com/a/64558717/5308983
0

It turned out that the issue related not to venv management but to how console_scripts have to be handled w.r.t. debugging: https://stackoverflow.com/a/64558717/5308983

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.