Skip to content Skip to sidebar Skip to footer

Turtle Does Not Run More Than Once In Jupyter Notebook

I am trying to run some turtle code in jupyter notebook. When I run the code once, the code runs fine. However, if I run the code again, Python Turtle Graphics freezes and closes (

Solution 1:

This is apparently a recurrent problem with Jupyter/iPython

You can install iPyTurtle that's should help a lot. Found on this medium article

You can also create a file containing your commands using %%file turtle_example.py in top of cell and in cell below, then run it using !python turtle_example.py. But this is not really great

Solution 2:

I was having this problem too! Try:

$ pip install ipyturtle

$ jupyter nbextension enable --py --sys-prefix ipyturtle


In the setup.py file, in lines 74 & 75, change

os.path.join(here, 'ipyturtle', 'static', 'extension.js'),

os.path.join(here, 'ipyturtle', 'static', 'index.js')

to

os.path.join(here, 'js', 'lib', 'extension.js'),

os.path.join(here, 'js', 'lib', 'index.js')


$ pip install -e .

$ jupyter nbextension install --py --symlink --sys-prefix ipyturtle

$ jupyter nbextension enable --py --sys-prefix ipyturtle


Should say 'Successfully installed ipyturtle' at the end

Solution 3:

If you want to run the turtle module multiple times in Jupyter try turtle.bye() at the end:

import turtle

bob = turtle.Turtle()
bob.forward(50)
turtle.done()

try:
    turtle.bye()
except:
    print("bye") 

(I added try/except because .bye() raises an Terminator Error)

Based on turtle.done() not working in Spyder

Solution 4:

ipyturtle seems to not work with JupyterLab or VS Code. Try ipyturtle3. With python3.6+:

python -m pip install ipyturtle3

Try the examples listed in this repo: https://github.com/williamnavaraj/ipyturtle3

https://pypi.org/project/ipyturtle3/

I found this to work in JupyterLab and VSCode

Post a Comment for "Turtle Does Not Run More Than Once In Jupyter Notebook"