AttributeError on 'tkinter' project - python

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.

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.

Python Turtle Module not Working on Windows 10

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.✌

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?

python 'module' object has no attribute 'declare_namespace'?

I run python repl, I can run this:
from ipin.rpc.client_factory import client_factory
but when I write this to file, then run, I got error as follw:
Traceback (most recent call last):
File "/tmp/parser.py", line 3, in <module>
from ipin.rpc.client_factory.client_factory import ClientFactory
File "build/bdist.linux-x86_64/egg/ipin/__init__.py", line 1, in <module>
File "/home/ipin/anaconda2/lib/python2.7/site-packages/setuptools-18.5-py2.7.egg/pkg_resources/__init__.py", line 83, in <module>
File "/tmp/parser.py", line 3, in <module>
from ipin.rpc.client_factory.client_factory import ClientFactory
File "build/bdist.linux-x86_64/egg/ipin/rpc/__init__.py", line 1, in <module>
AttributeError: 'module' object has no attribute 'declare_namespace'
From your traceback, it looks to me like your code is in a module named parser.py which is also a name of a module used within within the ipin package. Your module is shadowing the internal module, so when another part of the package tries to get the declare_namespace object from parser, it fails.
Renaming your program something other than parser should work around the issue, but really, it's not your fault. The real fix is for the package to not use relative imports that might be shadowed in this way (Python 3 doesn't allow implicit relative imports any more for exactly this reason).

Python 3: AttributeError: 'module' object has no attribute '__path__' using urllib in terminal

My code is runnning perfectly in PyCharm, but I have error messages while trying to open it in terminal. What's wrong with my code, or where I made mistakes?
import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
html = response.read()
print(html)
Output from terminal:
λ python Desktop\url1.py
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Desktop\url1.py", line 1, in <module>
import urllib.request
File "C:\Users\Przemek\Desktop\urllib.py", line 1, in <module>
import urllib.request
ImportError: No module named 'urllib.request'; 'urllib' is not a package
You called a file C:\Users\Przemek\Desktop\urllib.py, you need to rename it. You are importing from that not the actual module. rename C:\Users\Przemek\Desktop\urllib.py and remove any C:\Users\Przemek\Desktop\urllib.pyc.
It is not the file you are running but you have the file in the same directory so python checks the current directory first hence the error.
You sare shadowing the standard library package urllib by naming your source file urllib.py. Rename it!
The fact this works at all in Pycharm is an amazing feat of engineering on the PyCharm developers!
You can also use absolute imports (from __future__ import absolute_import) here; but in this case I don't think it'll help since your startup source name shadows the very library/package you are trying to use!
Also, this:
import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
Should be like this:
import urllib
with urllib.urlopen('http://python.org/') as response:

Categories

Resources