Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 247
Ricky Robinson Avatar asked Jan 16 '13 21:01

Ricky Robinson


People also ask

What is the meaning of import * in Python?

What is Importing? Importing refers to allowing a Python file or a Python module to access the script from another Python file or module. You can only use functions and properties your program can access. For instance, if you want to use mathematical functionalities, you must import the math package first.

How do I import a file into Python?

You need to tell python to first import that module in your code so that you can use it. If you have your own python files you want to import, you can use the import statement as follows: >>> import my_file # assuming you have the file, my_file.py in the current directory.


1 Answers

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

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

(taken here)

like image 118
Ricky Robinson Avatar answered Sep 29 '22 13:09

Ricky Robinson