Python - Date and Time: AttributeError: 'module' object has no attribute 'month'

后端 未结 5 1092
醉话见心
醉话见心 2021-01-20 21:41

This is my calendar code in Python and I have saved it to my folder as calendar.py.

import calendar
a = calendar.month(2016, 3)
print (a)
print          


        
相关标签:
5条回答
  • 2021-01-20 22:21

    Had the same challenge. If the issue is not with a similar file name, please note that the prevmonth and nextmonth functions have been deprecated https://bugs.python.org/issue35406

    You can still access them using the _prevmonth and _nextmonth functions, but note that they don't check if the month is valid (kindly refer to the link posted above for details), so you may need to write your assert statement before using them

    MONTH_NUMBER = 12 # enter your month number here
    assert MONTH_NUMBER in range(0,13)
    
    # continue the rest of your code
    
    0 讨论(0)
  • 2021-01-20 22:27

    The problem is that you used the name calendar.py for your file. Use any other name, and you will be able to import the python module calendar.

    0 讨论(0)
  • 2021-01-20 22:27

    Do not name the file as calendar.py and there should be no calendar.py file on the same path

    0 讨论(0)
  • 2021-01-20 22:29

    You might have given your file name as calendar.py. This is imported instead of the module in the standard library.

    Solution: Rename the file. Also, make sure to delete "calendar.pyc" file located in the same path where you are running the script.

    As a check, do the following to locate your module import calendar print(calendar)

    0 讨论(0)
  • 2021-01-20 22:32

    Renaming the file is not going to be enough, when you import the file a .pyc is created so even if you rename you are still going to have a .pyc with the same name in the folder so you are still going to be importing from that.

    Also if you have any calendar.py or .pyc in your path before the standard libs calendar you will end up importing that also so make sure there are no other files named calendar.py/pyc anywhere in your path.

    0 讨论(0)
提交回复
热议问题