PyCharm does not work as terminal

Hello
I am trying to execute a simple code with turtle.

 from turtle import * 
 
 def marche(h):
     left(90)
     forward(h)
     right(90)
     forward(h)
 
 def escalier(n, h):
     for k in range(n):
         marche(h)
 
 speed(5)            #parametrage de la vitesse de 1 lent à 10 rapide, 0 étant la vitesse la plus rapide
 shape("turtle")     #choix de la forme de la tortue
 pencolor("red")     #choix de la couleur du crayon
 pensize(4)          #épaisseur du crayon
 up()                #lever le crayon     
 goto(-150,-150)     # aller à la position (-200,-200)
 setheading( 0)      #orientation de la tortue vers l'Est / 90  Nord / 180 Ouest / 270 Sud
 down()              #poser le crayon
 escalier(5, 50)    
 exitonclick()       #indispensable en cas d'exécution dans un IDE comme Pyzo

This code works as expected in a terminal directly with python3 (on a mac os) or also with a venv.
But it does not works in PyCharm.

The windows “Python Turtle Graphic” open but nothing append then
no warning or error in the consol of pycharm

I am new to pycharm I miss something… but what?
Thanks for help

Merci Amine
En effet je suis francais :wink:

J’ai tenté de tester tes 3 conseil sans trop de succes…
1- l’ajout de done() ne change rien
2 - je n’ai apparemment pas de “mode Scientific” et j’ai cru voir sur le net que c’etait un plugin pour PyCharm Pro?
3 - Pour le Run in terminal, j’ai testé avec “Run with Python console” ou “Emulate terminal in output console” => Pas mieux…

Oups et avec la reponse a ta question :
Avant je travaillais sous vi sur une fenêtre terminal…
Je viens de passer a PyCharm et j’ai le fichier main.py ouvert et je lance l’execution avec le triangle vert

Juste avant exitonclick() essaye de rajouter :

update()

Et si ca ne fonctionne pas essaye de remplacer exitonclick() par :

mainloop()

essaye aussi de lancer le programme via un terminal externe a pycharme via python3 [Le path du fichier]

ni update() ni mainllop() ne change rien.
Je ne crois pas que c’est le prog qu’il faut modifier.
car comme dit avant le programme marche tres bien si je le lance depuis une fenetre Term avec

my-venv/bin/python main.py

Il doit y avoir un problème avec ma conf de pycharm
La fenetre Turtle s’affiche mais sans rien dedans ni les “shape” ni le “pencolor”

Je vais regarder comment le debugger marche car j’ai l’impression que cela se bloque au milieu du programme

C’est bien cela il se bloque sur la commande

speed(5)

et ne passe jamais sur la commande

shape(“turtle”)

Hahaha, désolé, je n’avais pas fait attention à la partie où tu mentionnes le terminal.

Néanmoins, sache que ce qui t’arrive avec PyCharm et Turtle est une erreur fréquente, due à la façon dont PyCharm gère l’affichage graphique.

(Corrected)

Hello,

I tested your script using the PyCharm terminal. It works for me if I rename the module other than turtle.py and if I import the turtle module as an alias. I named the test script tut.py.

Here is the script:

import os

os.chdir(r'C:\Desktop')  # since I saved the module to my Desktop

##from turtle import *
import turtle as tt

def marche(h):
    tt.left(90)
    tt.forward(h)
    tt.right(90)
    tt.forward(h)

def escalier(n, h):
    for k in range(n):
        marche(h)

tt.speed(5)  # parametrage de la vitesse de 1 lent à 10 rapide, 0 étant la vitesse la plus rapide
tt.shape("turtle")  # choix de la forme de la tortue
tt.pencolor("red")  # choix de la couleur du crayon
tt.pensize(4)  # épaisseur du crayon
tt.up()  # lever le crayon
tt.goto(-150, -150)  # aller à la position (-200,-200)
tt.setheading(0)  # orientation de la tortue vers l'Est / 90  Nord / 180 Ouest / 270 Sud
tt.down()  # poser le crayon
escalier(5, 50)
tt.exitonclick()

In the PyCharm terminal, I then enter:

py tut.py

… and I observe a window open up with the turtle climbing a red stair like profile to the right.

Thanks for trying Paul

I have also put your modified code in a new project in PyCharm (file main.py)
(I have removed the import os and the chdir to desktop)

when trying to launch it in the terminal with

python main.py

I get the same behaviour, the tutrle windows open but is empty.
in the Term I get a message

DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don’t rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.

but it seem’s when reading that is is not a probleme

I program is stuks in the “first” tt.xxx lines
with the prog like this it sucks at the tt.pencolor but if I put pencolor befor shape it stucs at shape…

I definitively think it is not by changing something in the program that it will works…
My PyCharm seem’s to be not well configure, or has a compatibility issue with turtle?

Ok, you can remove or comment out the import os and the os.chdir(r'C:\Desktop') in the python module.

Can you save the python script to the Desktop just for this test please so that we are on the same page. Then, in the terminal window, change the working directory to C:\Desktop via this command: chdir C:\Desktop.

When you run the script, then type:

py tut.py  # or whatever name you have given your script

It should run just fine. Btw, I am using Python v3.13 for my interpreter in Pycharm if that makes a difference.

We will not be exactly in the same configuration as I am on a MacOs :slight_smile:

I have put the tut.py script (without the import os and os.chdir lines
If I go in a Term MacOs in /Users/myname/Desktop (where tut.py is) and run

python3 tut.py

it works

if I go in the Term of PyCharm and run

chdir /Users/myname/Desktop
python3 tut.py
it works

But If I try to open the file tut.py in the Pycharm and push the green triangle to execute it, it does not works…

That is because the green triangle ONLY runs what is in an active Pycharm module. This is normal and to be expected.

So, the terminal runs files that are explicitly stated via the py my_file.py command assuming that you are in the working directory … the directory where you have the file stored (or if not in the active working directory, you would have to include its path along with its module name). If you open the tut.py in Pycharm window, then when pressing the green arrow will run the script module.

For example, in the following image, I have two script modules that I have opened in PyCharm.

test_script_modules

If I hit the green arrow on top, it will run the currently active Python module.

If you open your tut.py file in PyCharm, and you see its name on the above tabs, and click on the green arrow on top, it will run. Remember, the terminal is a substitue for the CMD terminal - separate from PyCharm. In other words, it is a tool of convenience that has been conveniently integrated into the PyCharm IDE environment so that you do not have to open a separate CMD window. It is technically NOT PyCharm.

Btw, if you already have an active script module opened in PyCharm, you can comment out the entire code above and then paste the tut.py code at the bottom. If you hit the green button, it should run. I use Windows. If I select code and press ctrl + /, it comments out the code. You can try to see if it is the same on a mac. If not, in the PyCharm IDE, go to the top menu and look for Code menu, and look for // Comment with Line Comment for keyboard short-cut.

Thanks for your help

Not sure to understand…
Below a screenshot when I select the green triangle → And when it does not works…

By the way the error seem’s to be link to turtle
I have perform a prog with pygame that works nice with the green triangle

Can you show the error please.

Oups exuce my english
When I say the error I mean “the tutrtle windows open in grey but nothing append”
No error Message in the consol

Which interpreter are you using. You can check here:

I am using the Python 3.9
There is 2 3.9 “version”
the /usr/bin/python3
the one in the venv ~/Desktop/tut/.venv/bin/python

I try both but they have the same behavior

Can you download the latest version from here (v3.13.3):

Once having done so, in PyCharm, go to: SettingsPython Interpreter (see image from previous post). To the right, it will have the option: Add Interpreter. To add the newly installed interpreter, go to where the interpreter was downloaded to. It might be to here (double check for your specific case):

C:\Program Files\Python313

Click on the folder icon as shown here:

select_interpreter

In the folder, select the python.exe file.

1 Like

Thanks a lot Paul
That was indeed a Python version problem
With 3.13.3 it works!!!

1 Like