Is debugging possible with python-click using intellij?
Asked Answered
T

1

11

This seems like a general problem with python-click, however there is no mention of it anywhere on google.

If I try run even the simplest of python click scripts, like the following from realpython

import click

@click.group()
def greet():
    pass


@greet.command()
def hello(**kwargs):
    pass


@greet.command()
def goodbye(**kwargs):
    pass

if __name__ == '__main__':
    greet()

The Intellij debugger completely bombs with the error message:

Error: no such option: --multiproc

I have tried this with multiple python-click scripts and debugging never works. Has anyone else noticed this and is there any way around this?

Treiber answered 1/9, 2016 at 19:35 Comment(5)
I can't reproduce this in PyCharm. I don't have access to IntelliJ, but if you can get your hands on a copy of PyCharm, that might be one option.Obturate
Thanks, I will try that!Treiber
@MorganThrapp Tried it on pycharm and same issue :(Treiber
What version of python? (i mean exact, not just 2 or 3)Retina
@FrankV Python 2.7.9 and Also shows this in the log pydev debugger (build 162.1812.1)Treiber
F
26

The problem arises when you do not pass any parameters to the click entry-point. In that situation, click asks a platform specific function to get it's args, get_os_args() which is unrelated to sys.argv.

The result effect is that debugger needed arguments are also passed to the click parser, effectively activating an error in click.

The solution is to explicitely pass sys.argv[1:] to the click entrypoint which will override the default get_os_args() behavior:

import sys
if __name__ == '__main__':
    greet(sys.argv[1:])
Floorboard answered 23/11, 2016 at 21:56 Comment(1)
Thanks for this. I ran into this when trying to debug an entry point within PyCharm (using Python 2.x on Windows). I also found this thread that points to the issues on why my specific case was failing. The author's solution here was to import click at the beginning of the script, avoiding this error. Not totally relevant in the OP's case, but thought it might save someone some grief.Rippy

© 2022 - 2024 — McMap. All rights reserved.