Python Turtle Module not Working on Windows 10 - python

I am new in programming. I have an issue. my python code is:
import turtle
turtle.forward(100)
turtle.exitonclick()
I already install turtle on my desktop. But VsCode Error message is:
Traceback (most recent call last): File "d:/Code
Practice/Python/turtle.py", line 1, in
import turtle File "d:\Code Practice\Python\turtle.py", line 3, in
turtle.forward(100) AttributeError: partially initialized module 'turtle' has no attribute 'forward' (most likely due to a circular
import)

The file you are working on has the same name as the turtle module. So, your file overwrites the module and now you are importing your own file. And that file has no forward function, so it isn't working.
Try renaming your file to something else and give it a try.

You have named you file turtle.py because of which python imports that file as the module and also runs the same file while running which creates a loop and gives that error Solution: Name your file something else. For example- turtle_.py or whatever else you want except for the modules name itself as that is reserved for the python interpreter.✌

Related

(AttributeError) installation problem with keyboard

I am having a issue when I try to use the keyboard module.
My code is:
import keyboard
keyboard.write('Hello.', 0.3)
And the error was:
C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe "C:/Users/user/Desktop/simple brain/keyboard.py"
Traceback (most recent call last):
File "C:\Users\user\Desktop\simple brain\keyboard.py", line 1, in <module>
import keyboard
File "C:\Users\user\Desktop\simple brain\keyboard.py", line 2, in <module>
keyboard.write('Hello.', 0.3)
AttributeError: partially initialized module 'keyboard' has no attribute 'write' (most likely due to a circular import)
Process finished with exit code 1
I used pip install keyboard in pycharm terminal.
How do I fix the installation of or properly install keyboard?
The name of your file is "keyboard.py". When you do import keyboard, python is trying to import your current file which is causing a circular import. Rename your file to something else.

AttributeError on 'tkinter' project

I get the following AttributeError:
Traceback (most recent call last):
File "C:\Users\thaku\OneDrive\Desktop\tkinter python\tkinter.py", line 1, in <module>
import tkinter
File "C:\Users\thaku\OneDrive\Desktop\tkinter python\tkinter.py", line 2, in <module>
win = tkinter.Tk()
AttributeError: partially initialized module 'tkinter' has no attribute 'Tk' (most likely due to a circular import)
this is my code snippet
import tkinter
win = tkinter.Tk()
win.title('GUI')
win.mainloop()
From the traceback I can see that you named your file tkinter.py which confuses python as it thinks that you are trying to import that file from itself. If you rename your file to something else it should work.
Well I looked into Traceback and found the problem.
You have named the file on which you have been working on as tkinter.py. While using the command import tkinter it imports your file (the one you are working on) itself rather than import the module tkinter.
The preferred answer would be that you rename the file you are working on as tkinter_pratice.py or something like that.
You can now learn that you can't give a file the same name as a module's name as if would import itself rather than importing that particular module.
You have named you file tkinter.py because of which python imports that file as the module and also runs the same file while running which creates a loop and gives that error
Solution:
Name your file something else.
For example:- tkinter_.py or whatever else you want except for the modules name itself as that is reserved for the python interpreter.

Why can't I import the re module?

I wanted to import the re module to do some web scraping.
I wrote down the 'import re' function and got this message:
Traceback (most recent call last):
File "/Users/willardsun/PycharmProjects/untitled/re.py", line 1, in <module><br>
import re<br>
File "/Users/willardsun/PycharmProjects/untitled/re.py", line 2, in <module><br>
re.compile<br>
AttributeError: partially initialized module 're' has no attribute 'compile' (most likely due to a circular import)
What does this exactly mean? I checked the binary skeleton and there was no re module. If the problem is due to this, then how do I install the module back? Thanks.
I think you are trying import re module in a .py file named 're.py'.
In this way, a name clash occurs.So why not change the name of the .py file into my_re.py?

randomchoicest() is giving attribute error

This is my python script:
import random
n=random.randint(1,100)
print(n)
This is the error I am getting while running the above script in linux mint terrminal:
Traceback (most recent call last):
File "ran1.py", line 2, in <module>
n=random.randint(1,100)
AttributeError: module 'random' has no attribute 'randint'
Also other attributes in random module like random.choice(), random.choices() give the same error.
Kindly help me out anyone.
If you have a file named random.py that you made, you may want to open it and look for the randint and choice functions in it, or the python is not installed correctly.

Code behaves different in IDLE and in py script - ghost.py

from ghost import Ghost
running it from IDLE works; but if I run a py file with only this line of code it get this error.
Traceback (most recent call last):
File "C:\Users\Teo1\Desktop\sub\ghost.py", line 1, in <module>
from ghost import Ghost
File "C:\Users\Teo1\Desktop\sub\ghost.py", line 1, in <module>
from ghost import Ghost
ImportError: cannot import name Ghost
It may seem a stupid question, but what it's so simple that I can't find my error.
I'm using ghost.py with PySide, both installed with pip.
Your program is trying to import itself, since the first module ghost it finds is your ghost.py.
Renaming your program will fix this problem.

Categories

Resources