D.Marvel
D.Marvel

Reputation: 21

Python import calendar error

I was doing tutorial from tutorialspoint about calendar


#!/usr/bin/python
import calendar

cal = calendar.month(2008, 1)
print "Here is the calendar:"

print cal

Above code throw the following error,

Traceback (most recent call last):

  File "./datetime.py", line 2, in <module>
    import calendar
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py", line 9, in <module>
    import datetime
  File "/Users/aungyin/workplace/python/datetime.py", line 4, in <module>
    cal = calendar.month(2008, 1)
AttributeError: 'module' object has no attribute 'month'

is it because of environment problem?

python is set to "/usr/local/bin/python" in $PATH

I am using Mac with Python of version 2.7.10.

Any suggestion?

Upvotes: 2

Views: 7827

Answers (2)

anoop-khandelwal
anoop-khandelwal

Reputation: 3860

Probably,you are giving the filename as datetime.py and so python compiler would treat as a new file. But in line import datetime ,compiler gets confused with the name of the file you gave(importing the same file you are in). So,change the filename for the desired behaviour like this

filename test_tutorials.py

 import calendar
 import datetime
 cal = calendar.month(2008, 1) print "Here is the calendar:"
 print cal

O/p Here is the calendar:
January 2008
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

Upvotes: 1

madhukar93
madhukar93

Reputation: 515

This code works for me, although you probably don't have two statements in one line. Don't name your file as datetime.py, your module name is in conflict with the prepackaged datetime module. Also, don't forget to remove the pyc file generated (It will be in the same directory as your source code file).

Upvotes: 2

Related Questions