Monday, October 6, 2025
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
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6827 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6783 POSTS0 COMMENTS