After changing the import as a from-import
i'm running into this error:
from datetime import datetime, date, timedelta
today = date.today()
from time import mktime
from feedparser import feedparser
import settings
def check_calendar():
d = feedparser.parse(settings.personal_calendar_feed)
for entry in d.entries:
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
Traceback (most recent call last):
File computer.py", line 734, in <module>
check_calendar()
File "computer.py", line 210, in check_calendar
if(date.fromtimestamp(mktime(entry.date_parsed))==today):
AttributeError: 'function' object has no attribute 'fromtimestamp'
It says
AttributeError: 'function' object has no attribute 'fromtimestamp'
in the error. Apparently you may have a function named "date" in your code. Since Python allows you to use any name, and new conflicting names will override the older ones.
Instead, when python can't find a function from a module or object, it usually says type object has no attribute or module has no attribute, like if I want to call "fromtimes" :
type object 'datetime.date' has no attribute 'fromtimes'
You may want to check your code carefully again.
It is highly possible that you have redeclared date as function def date(): earlier in code. Otherwise it makes no sense.
Related
I am using the "pdftables" library to extract tables from a pdf.
This is my code:
import pdftables
pg = pdftables.get_pdf_page(open("filename.pdf","rb"),253)
print(pg)
table = pdftables.page_to_tables(pg)
print(table)
I am getting this error and I am not sure what's causing it.
Traceback (most recent call last):
File "c:\Users\gayak\OneDrive\Documents\PDF to Database\PDF_to_Tables_3.py", line 9, in <module>
table = pdftables.page_to_tables(pg)
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\pdftables.py", line 485, in page_to_tables
box_list = LeafList().populate(page, flt).purge_empty_text()
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 98, in populate
for obj in children(pdfpage):
File "C:\Users\gayak\AppData\Local\Programs\Python\Python310\lib\site-packages\pdftables\tree.py", line 75, in children
if isinstance(obj, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
The version of python I am using is python 3.10.4
I used pip install pdftables.six to get the library
If you don't want to change the source code, there is an easier way. Just use this in your script after importing.
import collections
collections.Iterable = collections.abc.Iterable
As the Error says, the attribute isn't valid. When using collection.Iterable then it not finds the Iterable attribute. This can have different reasons but in this case, after looking on the package files on Github, i noticed that you are missing the abc keyword. I not tried this solution, but I'm 95% sure that using collections.abc.Iterable will do the thing.
import collections
from _collections_abc import Iterable
collection.Iterable = Iterable
This should work 🙂
A simple fix that works for python3.10:
Under directory
/usr/lib/python3.10/collections/__ init__.py
Note: The path might change depending
Add this line of code:
from _collections_abc import Iterable
I had create a Python package in my project with name text_analysis, and inside a class TextAnalysis with a method search_records
When I write a test in the main.py I can import TextAnalysis, but the ojbect (with IDE) doesn't show the method search_records. If I write the test.search_records(barcode) I got this error message:
Traceback (most recent call last):
File "C:..../main.py", line 19, in <module>
analysys = TextAnalysis(bib)
TypeError: 'module' object is not callable
Make sure that you are defining a class in TextAnalysis.py that contains the modules you want to call. If you would rather call individual modules without a class structure within TextAnalysis.py, call them as TextAnalysis.module_name().
Here is a simple example: Class vs Module structure
I'm using python version 3.6.
mystuff.py includes:
mystuff = {'donut': "SHE LOVES DONUTS!"}
mystuffTest.py includes this
import mystuff
print (mystuff['donut'])
The error that I receive when I run mystuffTest.py is as follows:
$ python3.6 mystuffTrythis.py
Traceback (most recent call last):
File "mystuffTrythis.py", line 3, in <module>
print (mystuff['donut'])
TypeError: 'module' object is not subscriptable
So far I haven't seen this exact error here on stackoverflow. Can anyone explain why I am getting this error?
import mystuff is importing the module mystuff, not the variable mystuff. To access the variable you'd need to use:
import mystuff
print(mystuff.mystuff['donut'])
EDIT: It's also possible to import the variable directly, using:
from mystuff import mystuff
print(mystuff['donut'])
I got this error because a later from __ import * statement imported a module which bound my variable to something else:
from stuff_a import d
from stuff_b import *
d['key']
In stuff_b.py, d was bound to a module, hence the error. Lesson learned: avoid importing * from modules.
I came across datetime module in python, as this is first time i need it in my scripts.
But I really have a problem with it, in example:
import datetime
date_now = datetime.date.today()
print date_now
As an answer i get:
Traceback (most recent call last):
File "datetime.py", line 3, in
import datetime
File "/root/Desktop/python_work/datetime.py", line 5, in
today = datetime.date.today()
AttributeError: 'module' object has no attribute 'date'
Then i checked my datetime module to list properties:
import datetime
for p in dir(datetime):
print p
As a result i got only:
> __builtins__
>
> __doc__
>
> __file__
>
> __name__
>
> __package__
>
> datetime
And yeah, true, 'module' object has no attribute 'date', it really do not have it.
Is there any idea, what should be added to python install or how to fix it?
The problem is that you've got a file of your own named datetime.py that's in your module path. You can see that from the traceback:
Traceback (most recent call last):
File "datetime.py", line 3, in
import datetime
File "/root/Desktop/python_work/datetime.py", line 5, in
today = datetime.date.today()
Instead of importing the datetime.py in your site-packages, you're importing the datetime.py in /root/Desktop/python_work.
In fact, it seems probable that datetime.py is the actual script you're running, in which case you're just importing yourself.
The solution is to rename the script. And, in general, do not give scripts the same name as modules in the stdlib—especially modules you plan to use.
Rename your file from datetime.py" to something else. This name is conflicting with the builtin datetime module. Hence the error.
THANK YOU
IT
SOLVED
MY PROBLEM. I HAD A FILE NAMED datetime.py I changed it to dt((DATETIME)).py
I am trying to initialize a time object like this:
t = datetime.time(0,0,0)
but I am getting this error:
descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
I have these things imported
import datetime
from datetime import datetime, date, time
import time
They seem a bit redundant so I am wondering if this is what is causing the problem
I am also using the strptime method and the combine method
earliest = datetime.combine(earliest, t)
value = datetime.strptime(value, format)
You can create the object without any values:
>>> import datetime
>>> datetime.time()
datetime.time(0, 0)
You, however, imported the class datetime from the module, replacing the module itself:
>>> from datetime import datetime
>>> datetime.time
<method 'time' of 'datetime.datetime' objects>
and that has a different signature:
>>> datetime.time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument
>>> datetime.time(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
Either import the whole module, or import the contained classes, but don't mix and match. Stick to:
import datetime
import time
if you need both modules.
The constructor for time is:
class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])
(from http://docs.python.org/library/datetime.html#time-objects)
This works for me:
In [1]: import datetime
In [2]: t = datetime.time(0, 0, 0)
In [3]: print t
00:00:00
It's the fact that you're importing a conflicting datetime from datetime. You probably meant time, except you're also importing a conflicting time. So how about:
import datetime as dt
and
t = dt.time(0, 0, 0)