Reputation: 274
I am using Calendar function in python for the first time this is my code:
import calendar
print ("The calender of year 2018 is : ")
print (calendar.calendar(2018,2,1,6))
print ("The starting day number in calendar is : ",end="")
print (calendar.firstweekday())
I am getting the followng output
The calender of year 2018 is :
Traceback (most recent call last):
File "C:/Users/AAKASH PATEL/Desktop/calendar.py", line 5, in <module>
import calendar
File "C:/Users/AAKASH PATEL/Desktop\calendar.py", line 10, in <module>
print (calendar.calendar(2018,2,1,6))
TypeError: 'module' object is not callable
How can i solve this
Upvotes: 3
Views: 2152
Reputation: 1124968
You named your script calendar.py
, so it is that file that is imported. You can see this in the traceback:
File "C:/Users/AAKASH PATEL/Desktop/calendar.py", line 5, in <module>
import calendar
File "C:/Users/AAKASH PATEL/Desktop\calendar.py", line 10, in <module>
print (calendar.calendar(2018,2,1,6))
So import calendar
imported calendar.py
, which now has an attribute calendar
, which is your script.
Rename your script to not mask the built-in library; for example, calendar_demo.py
would be fine. Once you do, it works perfectly, on my system, it outputs:
The calender of year 2018 is :
2018
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9
9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16
16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23
23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30
30 31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30
31
The starting day number in calendar is : 0
Note that calendar.calendar(2018)
would suffice; the defaults for the next three arguments (w
for the column width, l
for lines per week, and c
for the spacing between month columns) are 2, 1 and 6 respectively.
Upvotes: 2
Reputation: 109
When copying your code and executing it, it works perfectly fine! But I see that it seems your script, where you have that code is called calendar.py! Python then thinks this is what you want to import! So rename your script to something else!
Upvotes: 1
Reputation: 82795
Rename your script name from calendar.py
to calendarScript.py
"C:/Users/AAKASH PATEL/Desktop/calendar.py"
to
"C:/Users/AAKASH PATEL/Desktop/calendarScript.py",
Note: Do not name your script the same name as a module.
Upvotes: 4