Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

rich.pretty without ipython yields SyntaxError during source generation

See original GitHub issue
$ python -m nuitka --version
Commercial: None
Python: 3.9.14 (main, Sep  6 2022, 23:29:09) 
Flavor: Unknown
Executable: /.../venv/bin/python
OS: Darwin
Arch: x86_64

For this issue, Nuitka was installed from source into a venv. Python 3.9 from Homebrew. Same issue is apparent on 1.1 installed using pip. Also observed on Windows and Linux.


Nuitka’s antibloat module contains this optimization for rich.pretty:

Unfortunately, rich.pretty contains this block of code:

    try:  # pragma: no cover
        ip = get_ipython()  # type: ignore[name-defined]
        from IPython.core.formatters import BaseFormatter

The effect of the optimization is to yield the line:

        ip = raise NameError  # type: ignore[name-defined]

Which in turns causes a SyntaxError during processing.

Test script,

from rich.pretty import Pretty

if __name__ == "__main__":
        a = Pretty("hello")

Trimmed output of building:

$ python -m nuitka --standalone
Nuitka-Inclusion:WARNING: Cannot follow import to module 'rich.pretty' because of 'SyntaxError'.

And, naturally, the resulting binary doesn’t work, yielding a complaint about the above syntactic problem.

$ ./t.dist/t
Traceback (most recent call last):
  File "/.../t.dist/", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "/.../t.dist/rich/", line 1, in <module rich.pretty>
  File "/.../venv/lib/python3.9/site-packages/rich/", line 246
    ip = get_ipython()  # type: ignore[name-defined]
SyntaxError: invalid syntax

As a workaround, I can include the option --noinclude-IPython-mode=allow so it does not optimize that line; there is no ipython in the venv and this is not an ipython interpreter, so the result is still a NameError once that line is reached.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

kayhayencommented, Sep 29, 2022

A fix is coming in 1.1.1, thanks for your report.

kayhayencommented, Oct 2, 2022

Thanks for your report, this is fixed on the factory branch, which is a development version under rapid development. You can try it out by going here:

Feedback if this is working is very welcome, just please do not share plans of doing it, but rather confirmations or denials of it working.

I intend to push 1.1.1 today if everything else goes fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pretty Printing — Rich 12.6.0 documentation
Truncating pretty output​​ If you set the max_length argument to an integer, Rich will truncate containers with more than the given number of...
Read more >
python - How to programmatically generate markdown output ...
The functions you want are in the IPython.display module. ... f""" # This is an h1 Rich can do a pretty *decent* job...
Read more >
Robust Jupyter report generation using static ... - Ploomber
Generating robust reports using Jupyter and static analysis. ... share or review later but it's not a good choice as source code format....
Read more >
IPython Documentation
All of IPython is open source (released under the revised BSD license) ... --[no-]autoedit_syntax When a syntax error occurs after editing a ...
Read more >
Plotting and Programming in Python - Dr Toby Hodges
The Jupyter Notebook has two ways to get help. Every function returns something. Python reports a syntax error when it can't understand the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found