0

I have a problem with PyCharm. I start learning python with PyCharm and I want write some with turtle package. When i try execute this code

import turtle

bob = turtle.Turtle()

I have that message:

"C:\Program Files\Python36\python.exe" C:/Users/Ptr/Desktop/python/Proj_1/Gui.py
Traceback (most recent call last):
  File "C:/Users/Ptr/Desktop/python/Proj_1/Gui.py", line 1, in <module>
    import turtle
  File "C:\Program Files\Python36\lib\turtle.py", line 107, in <module>
    import tkinter as TK
  File "C:\Users\Ptr\Desktop\python\tkinter.py", line 3, in <module>
    okno = tkinter.Tk()
AttributeError: module 'tkinter' has no attribute 'Tk'

Process finished with exit code 1 

When I try execute that code using IDLE all works. In PyCharm I try install turtle in project:

Python>Project Interpreter

but all time I have same error:

Collecting turtle
  Using cached turtle-0.0.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Ptr\AppData\Local\Temp\pycharm-packaging\turtle\setup.py", line 40
        except ValueError, ve:
        except ValueError, ve:
                         ^
    SyntaxError: invalid syntax

    ----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in C:\Users\Ptr\AppData\Local\Temp\pycharm-packaging\turtle\
  enter code here
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Incl
  • 3
  • 1
  • 4
  • Just guessing, you may be using a 3.x interpreter from Pycharm and a 2.x from IDLE and may not have tkinter for python 3 installed. Run this code, both from idle and from pycharm and report what you get; import sys; import tkinter; print (sys.version) – SteveJ Jan 11 '18 at 22:22
  • Sure i run that code and for PyCharm is: "C:\Program Files\Python36\python.exe" C:/Users/Patryk/Desktop/python/Mysl_w_jezyku_python/Projekt_Interfejsu_53.py Traceback (most recent call last): File "C:/Users/Patryk/Desktop/python/Mysl_w_jezyku_python/Projekt_Interfejsu_53.py", line 2, in import tkinter File "C:\Users\Patryk\Desktop\python\tkinter.py", line 3, in okno = tkinter.Tk() AttributeError: module 'tkinter' has no attribute 'Tk' Process finished with exit code 1 For idle: 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] – Incl Jan 12 '18 at 12:49
  • Can you run the same on IDLE? I see you are running Python36 with Pycharm? I'm wondering if you have python 2.x installed and that is what is running with IDLE. In which case, you may not have the python3 version of tkinter installed - making it fail with Pycharm (running Python3.6), but run with IDLE (running Python 2.x). – SteveJ Jan 12 '18 at 16:24
  • Yes i run import sys; import tkinter; print (sys.version) on default IDLE 3.6.1 and all work. When I try execute that code in PyCharm i have error what i paste before. My Python interpreter screen shoot url: https://i.imgur.com/qpcSShq.png – Incl Jan 12 '18 at 16:35
  • the print statement should be giving you an output -- telling you the version of python. – SteveJ Jan 12 '18 at 16:37
  • Output from PyCharm: https://pastebin.com/J32raG3E Output for default Python IDLE 3.6.1: 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] – Incl Jan 12 '18 at 16:42
  • Ok, thank you -- I guess you already provided that, I just didn't see it in your previous. I guess it wasn't what I thought as you are using the same version of python. Do a search on 'AttributeError: module 'tkinter' has no attribute 'Tk', there is some stuff on SO. One of the causes is that people have a module named tkinter in their project (don't know if that applies). That it runs in idle and not in PyCharm doesn't leave much - either python version or module naming error? I'm afraid I don't have much else to offer, sorry. – SteveJ Jan 12 '18 at 17:26
  • Thanks for help - i do all what you say and all time same output :/ i dont know what i can do withit :/ thanks you for your time! If someone have same problem and know how solv it please answer – Incl Jan 12 '18 at 17:35
  • what is the name of the file you are using in pycharm? You can NOT have any file/module named turtle.py. I made the same mistake in testing and got a similar error. Make sure nothing in your project is named turtle or tkinter. – SteveJ Jan 12 '18 at 17:55
  • Thanks SteveJ! I check my files and yes i missclick and rename file like a tkinter.py and rename it solves this problem. Thanks for info and I'll be careful with naming files :D – Incl Jan 12 '18 at 23:47
  • Awesome, I'll write this up in an answer form in a way that will be useful for others in the future. If you can accept that answer once complete, that would be great. – SteveJ Jan 12 '18 at 23:49
  • Done. Thanks :) awesome feedback – Incl Jan 13 '18 at 22:12
  • Does this answer your question? [Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-with-the-same-name-raises-attributeerro) – Karl Knechtel Mar 06 '23 at 01:37
  • There are two unrelated problems here. The first one is because you should not use the name `tkinter.py` for your own code - that causes problems when the standard library `turtle` tries to import the standard library `tkinter`. The second is because you have asked Pycharm to use a package **from PyPI** called `turtle`, which has **nothing to do with** the standard library `turtle` for drawing. – Karl Knechtel Mar 06 '23 at 01:39

2 Answers2

0

You don't pip turtle. Turtle is a built-in python package. You just say import turtle.

ds_secret
  • 338
  • 3
  • 18
0

A couple of things to note for those running into this at a later time:

  • tkinter must be installed on your machine. There is a python2.x and python3.x version. It must match your version of python. For example, In Ubuntu, I am running python3.6, and had to run apt-get install python3.6-tk
  • Pycharm allows you to pick interpreters. It is possible to be running a different interpreter in IDLE than in PyCharm, making the mismatch of python and tkinter versions potentially confusing. This is further complicated potentially by virtual environments. Be certain your python version and you tkinter versions match.
  • You can not have modules in your import path of the name tkinter.py or turtle.py
  • As mentioned by ds_secret, turtle comes with python, no need to
    install it separately.
SteveJ
  • 3,034
  • 2
  • 27
  • 47