Saturday, July 25, 2026
HomeLanguagesInteractiveInterpreter runcode() in Python

InteractiveInterpreter runcode() in Python

With the help of InteractiveInterpreter.runcode() method, we can only execute the pre-compiled source code having single or multiple lines by using InteractiveInterpreter.runcode() method.

Syntax : InteractiveInterpreter.runcode(code)
Return : Return the result of executed source else error.

Example #1 :
In this example we can see that by using InteractiveInterpreter.runcode() method, we are able to execute the piece of code and if run is successful we can get the result else error by using this method.




# import code and InteractiveInterpreter
import code
from code import InteractiveInterpreter
  
source = 'print("GeeksForGeeks")'
compile_code = code.compile_command(source)
  
# Using InteractiveInterpreter.runcode() method
InteractiveInterpreter().runcode(compile_code)


Output :

GeeksForGeeks

Example #2 :




import code
from code import InteractiveInterpreter
  
source = 'a = 5; b = 5; li = [a * b for i in range(5)]; print(li)'
compile_code = code.compile_command(source)
  
# Using InteractiveInterpreter.runcode() method
InteractiveInterpreter().runcode(compile_code)


Output :

[25, 25, 25, 25, 25]

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6972 POSTS0 COMMENTS