I've a Python package package_name
which provides a command line application command-line-app-name
as console_script
:
setup.py
:
setup(
...
entry_points={"console_scripts": ["command-line-app-name=package_name.cli:main"]},
...
)
The virtualenv is located in <project>/.venv
and managed with pipenv
. pipenv
managed venvs should support VSCode debugging integration. I've created a debugger configuration launch.json
file with setting the Python path to the venv (pythonPath
):
{
// 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: command-line-app-name",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"program": "command-line-app-name",
"linux": {
"pythonPath": "${workspaceFolder}/.venv/bin/python",
"args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
},
"windows": {
"pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
},
"console": "integratedTerminal"
}
]
}
The Windows and Linux specific venv python executable and command line arguments should not have an impact. If I run the debugger I get: FileNotFoundError: [Errno 2] No such file or directory: '/home/florian/gitlab/package-name/command-line-app-name'
. It seems like I'm miss-interpreting the documentation somehow. I tried to find help w.r.t. vscode-python as well as debugpy without success. How can I debug a console script command line app (instead of a package module)?
console_scripts
cannot be debugged out-of-the-box. The solution is to call the entry point function directly instead ("program": "${workspaceRoot}/package_name/cli.py",
). This requires to add the if __name__ == '__main__':
idiom in the corresponding module (here: cli.py
). In my case the command line argument parser used is click
. However the pseudo-code should be very similar for other command line parser libs.
package_name/cli.py
:
@click.command()
@click.option(...)
def main(<args>, <kwargs>):
...
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter
.vscode/launch.json
:
{
// 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: command-line-app-name",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"program": "${workspaceRoot}/package_name/cli.py",
"linux": {
"pythonPath": "${workspaceFolder}/.venv/bin/python",
"args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
},
"windows": {
"pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
},
"console": "integratedTerminal"
}
]
}
NOTE: The tool used to manage the venv makes a difference. This solution does work in case the venv is managed with pipenv
. The solution does not work in case the venv is managed with poetry
.