randomchoicest() is giving attribute error - python

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.

Related

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?

Old error message keep popping up on IDLE

For some reason, anytime I try to import a python library, a particular old error message keeps popping up.
However, everything works fine when working with shell.
I know this is not a real programming question, but I'm stuck and would appreciate any help.
Any ideas on how to fix this?
Error message
>>> import nltk
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import nltk
File "C:\Python27\lib\site-packages\nltk\__init__.py", line 91, in <module>
from internals import config_java
File "C:\Python27\lib\site-packages\nltk\internals.py", line 22, in <module>
except ImportError: from xml.etree import ElementTree
ImportError: No module named etree
Does "C:\Python27\lib\xml\etree\ElementTree.py" exist in your computer?
Since IDLE and shell behaves differently, you may try the two lines in IDLE and Shell:
import sys
sys.path
Then check the difference of env path

Cygwin gives error using python

I want to execute below command but cygwin gives the error.
Please help me.
Python makeReadingsFile.py eichstaett.net.xml test.readings.xml
Traceback (most recent call last):
File "makeReadingsFile.py", line 75, in <module>
import argparse
ImportError: No module named argparse
As noted, the error message
$ Python makeReadingsFile.py eichstaett.net.xml test.readings.xml
Python: can't open file 'makeReadingsFile.py': [Errno 2] No such file or directory
occurs because there's no such file where you're telling it to look for one. Your second command looks to my eyes just like the first command:
Python makeReadingsFile.py eichstaett.net.xml test.readings.xml
Traceback (most recent call last):
File "makeReadingsFile.py", line 75, in <module>
import argparse
ImportError: No module named argparse
and you say that this is using the full address path, but since the given path is the same I think you must mean something like "when you change to the right directory". Anyway, the error message here is probably due to the fact you're using Python 2.6 or before: the argparse module was only introduced in Python 2.7.

Feedparser stopped working

Yesterday I installed feedparser (on OSX 10.5) and it worked fine, but now it stopped working.
This is the script (copied from feedparser documentation)
import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
d['feed']['title']
u'Sample Feed'
It tells me this:
Traceback (most recent call last):
File "example.py", line 3, in <module>
import feedparser
File "example.py", line 2, in <module>
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
AttributeError: 'module' object has no attribute 'parse'
But also an actual script using feedparser stopped working, same error.
The point is when there is a script named feedparser.py, python will considered it as a module to import with higher priority than the module installed.
Issue is with Name of file. Python confuses between name of file and module name.

Categories

Resources