Reputation: 4379
How does one open an interactive Python console/shell/prompt within Google Colab? Is it possible? An iPython prompt would be ideal but a regular prompt would suffice.
Also, it would be significantly more useful if the console's interpreter shared access to the variables/state with the Colab notebook's kernel.
Upvotes: 3
Views: 3499
Reputation: 157
You can write this code in your Colab file:
import sys
twenty = 20 # Example of variable that you can access from debug function
def debug():
sys.tracebacklimit = 0 # If error occurs, doesn't make screen look cluttered, you can always remove/comment out
exiting = False
while not exiting:
command = input("Command - ")
if command == "exit": # Make sure when you want to exit that you exit the debug function
exiting = True
else:
try:
eval(command)
except Exception as e:
print(f"There was an error: {str(e)}")
debug()
This asks for the command, then evaluates it. Call it with function debug
and exit by typing exit
.
Typing print(twenty)
outputs 20
because it is defined in the Python code.
Upvotes: 0
Reputation: 2282
!jupyter console --existing
appears to hang with no output because it is waiting to acquire the GIL in the same process as the notebook's runtime that is executing the ! magic, which isn't going to give it up until the ! is done. For the same reason I don't think it's going to be easy to get a subprocess invocation to share state with the notebook's runtime.
Obvs. you could program your own input() + eval() in a loop to simulate a py prompt that shared the env with your notebook.
Upvotes: 1