1

I am working on a calendar (this is a school assignment) The assignment is, Given a month and the day of the week that’s the first of that month entered as arugments on the command line, write a program that prints a calendar for the month. So, if I want to print Sept and it starts on Thursday, I would enter "python calendar.py 9 5" in the command line, and get a calendar that starts september on Thursday. Here's the code:

#!/usr/bin/python

import sys
import calendar

day = int(sys.argv[1])
month = int(sys.argv[2])
cal = calendar.TextCalendar(day)
print(cal.formatmonth(2018, month)

The error I get is File "calendar.py", line 4, in < module> import calendar

File "/home/myname/calendar.py", line 5, in < module> import TextCalendar

ImportError: No module named TextCalendar

Can someone tell me how to correct this? Thanks for any help!

  • 2
    what is `calendar` that you're importing? if this is in fact another file, please post the contents in your question. – R Balasubramanian Jul 10 '18 at 16:46
  • @RBalasubramanian It's the python calendar module that can be imported to work with dates. I'm still relatively new to python (just started learning it 3 days ago), but it is not another file. Here's the best website I can find for it https://pymotw.com/2/calendar/ – AngelOfChaos Jul 10 '18 at 17:34
  • @AngelOfChaos See my answer below. The issue is that you appear to have a file named calendar.py in your home directory that is being imported instead of the standard library implementation. For imports from the standard library, you don't need to download or install anything else, and depending on how your project is organized it's possible to create confusion about what you intend to import. That appears to be the case here. – Logan Bertram Jul 10 '18 at 17:39

1 Answers1

2

Did you intend to import from calendar.py in your home folder rather than the standard library calendar.py? If not, try renaming or removing your custom calendar.py.

Logan Bertram
  • 1,874
  • 1
  • 16
  • 22
  • That worked great! I renamed it to printCal, and no more problems. Now I feel kind of dumb. Thanks!! I do have another (sort of) related question. Can you tell me how to get this to print to a text file instead of the console? I have tried several different methods I found online, but none seem to work, or maybe I'm just not implementing them correctly – AngelOfChaos Jul 10 '18 at 17:40
  • @AngelOfChaos It's no problem! Python does a lot for you, but that also means some of the stuff under the hood isn't immediately evident. You can learn more about modules, packages, and importing [here](https://docs.python.org/3/tutorial/modules.html) – Logan Bertram Jul 10 '18 at 17:44
  • 1
    @AngelOfChaos: new questions are best asked separately, and not in a comment ("while I have your attention"). As a newly registered user, you probably want to read the [tour] some time and browse the [Help]. – Jongware Jul 10 '18 at 18:01
  • 1
    @AngelOfChaos To add to that, asking in a new question is important because SO strives to be a comprehensive knowledgebase. Every question here is as much for future readers as it is for the person asking. That said, asking this as a new question would make a duplicate of [this](https://stackoverflow.com/questions/28223774/redirect-print-output-to-file-in-python) which answers your question. – Logan Bertram Jul 10 '18 at 18:09