question-mark
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.

Bug: define_macro & embed doesn't work as expected inside a method vs __main__

See original GitHub issue

If I have a macro inside a function doesn’t work as expected, the same is working fine on __main__ condition. Why?

Define Macro in function throws an error…!

import IPython as ipy
from IPython.terminal.interactiveshell import TerminalInteractiveShell

def run():
    shell = TerminalInteractiveShell.instance()
    shell.define_macro('foo', """a,b=10,20""")
    ipy.embed(display_banner=False)

if __name__ == "__main__":
    run()
⋊> ~/c/i/i/ipycli on master ⨯ ipython test
In [1]: foo
Out[1]: IPython.macro.Macro('a,b=10,20\n')
In [2]: a,b
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-2fbdd1b4fa70> in <module>
----> 1 a,b
NameError: name 'a' is not defined

Define Macro in main works fine…!

import IPython as ipy
from IPython.terminal.interactiveshell import TerminalInteractiveShell

if __name__ == "__main__":
    shell = TerminalInteractiveShell.instance()
    shell.define_macro('foo', """a,b=10,20""")
    ipy.embed(display_banner=False)
⋊> ~/c/i/i/ipycli on master ⨯ ipython test
In [1]: foo
In [2]: a,b
Out[2]: (10, 20)

IPython version 7.14.0 OS: macOS Catalina Terminal: iTerm2 version 3.3.9

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kirankotaricommented, Jun 13, 2020

Got the solution, after defining the I need assign manually when I am calling the embed from function x, y = shell.user_ns['x'], shell.user_ns['y'] before ipy.embed(display_banner=False)

0reactions
kirankotaricommented, Jun 13, 2020

@Carreau updated the question with proper code, please help @minrk @takluyver

Read more comments on GitHub >

github_iconTop Results From Across the Web

Indent function doesn't work as expected when indenting tables
To workaround this issue you can use the User Macro defined in this comment and embed the table into nested 'indent' macros.
Read more >
c preprocessor - #define macro for debug printing in C?
Basically, it means that all of your code is checked by the compiler every time you compile - whether for release or debugging....
Read more >
Macros - Scala 3
Defining a macro and using it in a single project​​ It is possible to define macros and use them in the same project...
Read more >
std::embed - All the Details - The Pasture
The argument that storing binary data easily in a programming language – whose fundamental job is to interpret binary data to do its...
Read more >
Groovy Language Documentation
When a method (whether implemented in Java or Groovy) expects a java.lang. ... List , as Groovy doesn't define its own collection classes....
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found