Search code examples
pythonfunctionscopevisibilitypython-import

code.interact and imports/definitions visibility


I don't quite understand where imports and function definitions are visibile in a python module. Here's a simplification of my case:

from scapy.all import *

def getA():
    return 0

def getB():
    return getA() + 1

def getC():
    code.interact(local=locals()) 
    return 3

def main():
    print getA()
    print getB()
    print getC()
    exit()

if __name__ == '__main__':
    main()

Now, everything goes smoothly until I reach function getC and a command prompt appears, a lot of what I should see is missing.

  • getA() and getB() aren't visible
  • scapy, which was in the imports, isn't visible either

Why does this happen? What am I getting wrong?


Solution

  • As I wrote in a comment above, the solution is:

    code.interact(local=dict(globals(), **locals())) 
    

    (taken here)