AttributeError in Python - python

My whole code is
import warc
f = warc.open("file.warc.gz")
for record in f:
print record['WARC-Target-URI'], record['Content-Length']
But it gives me this error:
File "warc.py", line 1, in <module>
import warc
File "/path/warc.py", line 3, in <module>
f = warc.open("file.warc.gz")
AttributeError: 'module' object has no attribute 'open'
I just installed warc with sudo pip install warc, following these instructions. The code too is just a copy and paste from that page.
I don't understand why the import doesn't work. I tried to upgrade the installation too, but it's already up to date.

I finally discovered the problem reading this question. I had called my script warc.py, and that generated the conflict. Careful how you name your files.

Related

Python tempfile.mkstemp(): AttributeError: 'function' object has no attribute 'mkstemp'

Not my code, I was trying a tool that includes the osascript library.
In the lib, at the line path = temp.tempfile() the error in the title is raised.
The temp library, correctly imported by the authors, has the following code itself:
f, path = tempfile.mkstemp()
The tempfile.mkstemp() part works like a charm.
So, since we are talking about two very common and widely used libs and since I'm apparently unable to find anything relevant on Google, I'm kind pretty sure there's some problem with my local configuration.
I'm on macOS, using python3 (3.7), no virtual envs.
The command pip list --outdated tells me that exactly those two libs are actually outdated, but apparently there is no way pip will download the updated versions. (note: my pip is correctly referring to the python3 binary, not macOS outdated python 2.7)
osascript 0.0.0 2020.7.2 sdist
temp 0.0.0 2020.7.1 sdist
Any ideas?
Steps to reproduce the error:
>>> import temp
>>> temp.tempfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/temp/__init__.py", line 15, in tempfile
f, path = tempfile.mkstemp()
AttributeError: 'function' object has no attribute 'mkstemp'
This is a bug in package temp which is a dependency of osascript. The package imports tempfile and then immediately overwrites it with a function tempfile. Report the bug.

unable to import chatterbot in python

python app.py
Traceback (most recent call last):
File "app.py", line 1, in <module>
from chatbot import chatbot
File "C:\Users\hp\Desktop\try_projects\chat_bot\chatbot.py", line 1, in <module>
from chatterbot import ChatBot
ModuleNotFoundError: No module named 'chatterbot'
Code is
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer, ChatterBotCorpusTrainer
chatbot= ChatBot('Crazy')
what might be the solution to this problem?
python version: 3.8.3
Try installing the previous version of ChatterBot.
pip install chatterbot==1.0.4
This should work, unless there are some other problems. I had the same problem and it worked for me.
There would be another problem if you are using Python 3.8.x . In Python 3.8.x, a few functions of a few modules were removed.
You will be able to import ChattberBot , but when you name the bot, there will be an error.
File "C:\Python38\lib\site-packages\sqlalchemy\util\compat.py", line 264, in <module>
time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
Copy the location for the file given in the last line, where the error occurs.
C:\Python38\lib\site-packages\sqlalchemy\util\compat.py
Open file with IDLE or whatever editor you have. Please do Not open the file directly(this will run the file, and you will not be able to see the code) , instead Open with IDLE or Your Text editor
Then , go to line 264 in that . It would be written
time_func = time.clock
Instead of this change it to
time_func = time.perf_counter()
I Hope this helped! This was my first answer on StackOverflow !

Textract - AttributeError: 'module' object has no attribute 'process' - pointing to non existent file

I am trying to run a few speed tests on various ways of getting text out of .doc, .docx and .pdf files and came across textract for Python which looked promising. I have installed textract and all its dependencies on Kali Linux (Debian), i am just trying to run a simple test:
import os
import sys
import textract
text = textract.process("/home/owain/Documents/Documents/file.doc")
I named the file textract.py which i then found out breaks things: Converting .doc to pure text using Python
the error i am getting
kali#Kali:~/Scripts/TestScripts$ time python tttt.py
Traceback (most recent call last):
File "tttt.py", line 3, in <module>
import textract
File "/home/owain/Scripts/TestScripts/textract.py", line 4, in <module>
AttributeError: 'module' object has no attribute 'process'
So i removed textract, deleted the textract.py file and re-installed textract. This didnt work as i am still getting the same above error, even with that file not existing anymore. Unsure where to go from here so any help would be much appreciated!
This happens because you have a file called textract which collides with the actual dependency name.

Python is returning an AttributeError while importing some modules all of sudden. How do I fix it?

My script was running well before. I had a series of crashes on some scripts where I had to fix some things. However, all of a sudden, I can't even run a script and my sublime text shell is displaying this error. I tried chasing down the files in my /Library/...python... files to see if any files got renamed or any part of the module scripts got renamed or something, but have yet to figure it out. What can I try. Or better question, what did I do all of a sudden to cause this, and how would I undo it? It was literally working fine less than an hour ago not to mention all day.
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numerictypes.py", line 101, in <module>
import numbers
File "/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line 2, in <module>
import Quandl
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/__init__.py", line 11, in <module>
from .Quandl import (
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/Quandl.py", line 11, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas/src/numpy.pxd", line 157, in init pandas.hashtable (pandas/hashtable.c:22984)
AttributeError: 'module' object has no attribute 'dtype'
EDIT:
Thanks to the comments! I had tried re-installing pandas, numpy, and Quandl. But it was all due to the fact I had renamed a helper script numbers.py, when it needed to draw that from within. Hint to all future users: Do not rename python scripts to existing module types.
I've had a similar error that was caused by installing a package while I had IPython running. Try reinstalling Pandas using pip (make sure all Python instances are closed) and maybe you'll have to reiinstall hashtable as well, I can't remember.
try to reinstall this Quandl library
File
"/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line
2, in
import Quandl
pip uninstall Quandl
pip install Quandl
This problem occurred with me in spite of not naming the script name to the existing module name. I tried the following and it worked.
Instead of "import Quandl", I did "from Quandl import Quandl" and it worked for me!

Pycurl Error : AttributeError: 'module' object has no attribute 'Curl'

Brand new raspberry pi using the debian image from the pi site.
I used sudo apt-get install python-pycurl
My script looks like this
import pycurl
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.SSL_VERIFYPEER, 1)
c.setopt(c.CAINFO, '/etc/ssl/certs/ca-certificates.crt')
c.setopt(c.URL, 'https://theurl.com')
c.setopt(c.USERPWD, 'user:pass')
c.setopt(c.POSTFIELDS, 'Field1=This&Field2=That')
c.perform()
I'm getting this
Traceback (most recent call last):
File "pycurl.py", line 1, in <module>
import pycurl
File "/home/pi/test/pycurl.py", line 3, in <module>
c = pycurl.Curl()
AttributeError: 'module' object has no attribute 'Curl'
Look at the path in the traceback. It looks like you may be importing your own module called pycurl.py, not the actual pycurl library. Try renaming that file to something else so Python imports the real pycurl.
python first checks in the current directory for a module, then in the python directory.
rename your file to mypicurl.py or something. otherwise, you're just importing the script.
edit: i just saw your comment, and this means you havn't installed it properly. try a reinstall or install from a .deb

Categories