Python. from dateutil.relativedelta import * works in shell but not in a script

2.2k views Asked by At

When I go to run

from datetime import *; from dateutil.relativedelta import *

from a python script I get a error which to me seems to indicate a problem with the module itself.

Traceback (most recent call last):
  File "C:\Users\Tom\Documents\datetime.py", line 1, in <module>
    from datetime import *
  File "C:\Users\Tom\Documents\datetime.py", line 2, in <module>
    from dateutil.relativedelta import *
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\site-packages\python_dateutil-2.0-py3.5.egg\dateutil\relativedelta.py", line 11, in <module>
    import calendar
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\calendar.py", line 47, in <module>
    class _localized_month:
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\calendar.py", line 49, in _localized_month
    _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
  File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\calendar.py", line 49, in <listcomp>
    _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
AttributeError: module 'datetime' has no attribute 'date'

This however, does not happen when I use the exact same statement in the python shell, if used in the shell it works perfectly.

I did find that it directly related to relativedelta part of dateutil, as

from dateutil import *

works fine.

I have tried reinstalling dateutil but thats pretty much it.

Running python 3.5.1 with dateutil 2.0 if it matters.

EDIT: SORRY I'M AN IDIOT, my file was called datetime.py which was what caused the problem.

1

There are 1 answers

0
Addy On BEST ANSWER

I believe it's the source file name. I am guessing when python interpreter tries to find datetime module it would search the local directory first and the datetime module would be your source file than the datetime global package. this post explains more

Once you would rename your file to say dd.py instead of "C:\Users\Tom\Documents\datetime.py" it should work out.