Ariel
Ariel

Reputation: 988

Python error in script but works in jupyter notebook: TypeError: 'module' object is not callable

I am getting the below error when running a script that creates a list of holidays in variable h. I first ran the script on jupyter notebook and it worked fine with no errors, however when I run it as a script it does not work.

I looked at several posts to resolve the issue, but I am specifying what I am importing in the script so not sure why it still does not work.

Error

Traceback (most recent call last):
  File "\\user\config\workspace\ExcelProjects\src\root\nested\scrap.py", line 53, in <module>
h = USBankHolidayCal.holidays(datetime(2016, 1, 1), datetime(2016, 12, 31))
TypeError: 'module' object is not callable  

Code

from pandas.tseries.holiday import Holiday, AbstractHolidayCalendar, nearest_workday, MO, USFederalHolidayCalendar
from pandas.tseries.offsets import DateOffset, CDay
UKdays = []


class UKHoliday(AbstractHolidayCalendar):
rules = [
    Holiday('Boxing Day', month=12, day=26, year=2016,observance=nearest_workday),
    Holiday('Christmas Day', month=12, day=27, year=2016,observance=nearest_workday),
    Holiday('New Years Day', month=1, day=2, observance=nearest_workday),
    Holiday('Good Friday', month=4, day=14, observance=nearest_workday),
    Holiday('Easter Monday', month=4, day=17, observance=nearest_workday),
    Holiday('Early May', month=5, day=1, observance=nearest_workday),
    Holiday('Spring Bank', month=5, day=29, observance=nearest_workday),
    Holiday('Summer Bank', month=8, day=28, observance=nearest_workday),
    Holiday('Christmas Day', month=12, day=25, observance=nearest_workday),
    Holiday('Boxing Day', month=12, day=26, observance=nearest_workday),
]
UKBankHolidayCal = UKHoliday()
h = UKBankHolidayCal.holidays(datetime(2016, 1, 1), datetime(2016, 12, 31))
for x in h:
    UKdays.append(x.date())

Any help greatly appreciated!

Upvotes: 0

Views: 785

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140307

if you do:

import datetime
datetime(2016, 1, 1)

you get

TypeError: 'module' object is not callable

But if you do

from datetime import datetime
datetime(2016, 1, 1)

it works.

So the problem resides in the way you're importing datetime. You have to use the 2nd version in your case, or you're mixing up the module with the object, which share the same name.

Upvotes: 1

Related Questions