Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make typer traceback look normal

When using typer to parse CLI arguments, I get very verbose and colorful error messages. How can I get a normal Python traceback?

See screenshot for an example traceback (just the first few lines) for illustration of the verbose style:

❯ python scripts/add_priors.py             
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /Users/corneliusromer/code/nextclade_data_workflows/sars-cov-2/scripts/add_priors.py:26 in main  │
│                                                                                                  │
│   23 │   import polars as pl                                                                     │
│   24 │                                                                                           │
│   25 │   priors = (                                                                              │
│ ❱ 26 │   │   pl.scan_ndjson(ndjson, infer_schema_length=10000)                                   │
│   27 │   │   .select(                                                                            │
│   28 │   │   │   [                                                                               │
│   29 │   │   │   │   pl.col("nearestNodes"),                                                     │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │   json = <module 'json' from                                                                 │ │
│ │          
like image 576
Cornelius Roemer Avatar asked Dec 14 '25 10:12

Cornelius Roemer


2 Answers

You can disable it on a one-off basis by setting the environment variable _TYPER_STANDARD_TRACEBACK=1.

Disabling rich exceptions is possible by passing the kwarg pretty_exceptions_enable=False when initializing typer:

import typer

app = typer.Typer(pretty_exceptions_enable=False)

@app.command()
def main():
    raise Exception("test")

if __name__ == "__main__":
    app()

See the documentation for more options

like image 89
Cornelius Roemer Avatar answered Dec 17 '25 00:12

Cornelius Roemer


Also, if you're not using sub-command. i.e. using just typer.run(main) then create an instance of typer regardless and run the main command explicitly.
e.g.

app = typer.Typer(pretty_exceptions_enable=False)
app.command()(main)
app()

Note: I was going to leave it as comment below accepted answer but I can't add multiple lines in comment and python is picky about spaces/newlines.

like image 23
shriek Avatar answered Dec 17 '25 00:12

shriek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!