0

I'm trying to use the calendar module in my Django views.py file. If I make the import at the top of the file, with the rest of my package imports, and call the monthrange function on the module, I get the following:

'function' has no attribute 'monthrange'

If however I import the module in my function, where I make use of monthrange it works fine.

This has left me scratching my head. Any ideas?

1
  • Are you running the code in the same manner in both scenarios? First thing I usually check is your PYTHONPATH environment variable. Commented Jun 17, 2011 at 4:10

1 Answer 1

2

Seems like namespace clash - maybe 'calendar' got redefined some place as a variable or function name.

Try to import monthrange from calendar at the top of the module, 'monthrange' is much less likely to clash with a variable or function elsewhere the code.

from calendar import monthrange
Sign up to request clarification or add additional context in comments.

6 Comments

As far as namespace clashing goes, I'm importing calendar in another file which is being imported in views.py - could that be causing the issue?
from somemodule import * is evil.
@Michael: yes, this is a circular dependency issue rather than a namespace clash.
@Daniel: If that's the case, shouldn't I be able to just use calendar in my views.py without explicitly importing it?
@Michael what? you still need to import a function before you can use it in a particular file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.