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
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
''''
from datetime import datetime
now = datetime.now().time()
print(now)
o/p: 21:44:22.612870
''''
But, when i am trying:
''''
import datetime
now = datetime.now().time()
print(now)
''''
it give following error:
Traceback (most recent call last):
File "D:/3. WorkSpace/3. Django/datemodel/first.py", line 9, in
now = datetime.now().time() # time object
AttributeError: module 'datetime' has no attribute 'now'
any one explain what is difference between both?
The datetime library exports a module called datetime.
Modules are Python .py files that consist of Python code. Any Python file can be referenced as a module.
if you want you can also use it this way:
import datetime
datetime.datetime.now()
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.
FIXED: turns out there is a module already called parser. Renamed it and its working fine! Thanks all.
I got a python NameError I can't figure out, got it after AttributeError. I've tried what I know, can't come up with anything.
main.py:
from random import *
from xml.dom import minidom
import parser
from parser import *
print("+---+ Roleplay Stat Reader +---+")
print("Load previous DAT file, or create new one (new/load file)")
IN=input()
splt = IN.split(' ')
if splt[0]=="new":
xmlwrite(splt[1])
else:
if len(splt[1])<2:
print("err")
else:
xmlread(splt[1])
ex=input("Press ENTER to Exit...")
parser.py:
from xml.dom import minidom
from random import *
def xmlread(doc):
xmldoc = minidom.parse(doc)
itemlist = xmldoc.getElementsByTagName('item')
for s in itemlist:
print(s.attributes['name'].value,":",s.attributes['value'].value)
def xmlwrite(doc):
print("no")
And no matter what I get the error:
Traceback (most recent call last):
File "K:\Python Programs\Stat Reader\main.py", line 10, in <module>
xmlwrite.xmlwrite(splt[1])
NameError: name 'xmlread' is not defined
The same error occurs when trying to access xmlwrite.
When I change xmlread and xmlwrite to parser.xmlread and parser.xmlwrite I get:
Traceback (most recent call last):
File "K:\Python Programs\Stat Reader\main.py", line 15, in <module>
parser.xmlread(splt[1])
AttributeError: 'module' object has no attribute 'xmlread'
The drive is K:\ because it's my personal drive at my school.
If your file is really called parser.xml, that's your problem. It needs to be parser.py in order to work.
EDIT: Okay, since that wasn't your issue, it looks like you have a namespacing issue. You import your parser module twice when you use import parser and then from parser import *. The first form of it makes "parser" the namespace and the second form directly imports it, so in theory, you should have both parser.xmlwrite and xmlwrite in scope. It's also clearly not useful to import minidom in main.py since you don't use any minidom functionality in there.
If you clear up those and still have the issue, I would suggest looking at __ init __.py. If that still does nothing, it could just plain be a conflict with Python's parser module, you could substitute a name like myxmlparser.
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.