[Fwd: [Tutor] import calendar not working]

Michael Perry vball@erols.com
Tue, 26 Oct 1999 19:24:40 -0400


This is a multi-part message in MIME format.
--------------6029D8F58CB429C034E37EFD
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I was under the (apparently mistaken) impression that calendar.py was
part of the standard release.  I did not develope it.  If it is not part
of the standard release it may have come with HTMLgen, although HTMLgen
seems to install itself in pythton1.5/site-packages.  Comparing the
dates it looks like that module was installed the same time as
everything else.

I've attached a script where I've copied the file to /tmp, run it from
my current dir, then run it from /tmp.  It works in /tmp but not the
current dir.

It seems like its my environment but I don't see where.  I've attached
calendar.py also.


The program runs fine from my home directory

[mike@spin python1.5]$ ls -ld c*
-rw-r--r--   1 root     root         4442 Mar 21  1999 calendar.py
-rw-r--r--   1 root     root         6033 Mar 21  1999 calendar.pyc
-rw-r--r--   1 root     root         5658 Mar 21  1999 calendar.pyo
-rwxr-xr-x   1 root     root        45830 Mar 21  1999 cgi.py*
-rw-r--r--   1 root     root        52720 Mar 21  1999 cgi.pyc
-rw-r--r--   1 root     root        50758 Mar 21  1999 cgi.pyo
-rw-r--r--   1 root     root         2090 Mar 21  1999 cmd.py

Here's the version information:
Python 1.5.1 (#1, Mar 21 1999, 22:49:36)  [GCC egcs-2.91.66 19990314/Li
on linux-i386


[mike@spin mike]$ more cal.py
#!/usr/bin/python


print "Content-type: text/html" # HTML is following
print                           # blank line, end of headers

import calendar
import HTMLcalendar

calendar.prmonth(1999, 10)
[mike@spin mike]$ ./cal.py
Content-type: text/html

    October 1999
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
[mike@spin mike]$




--------------6029D8F58CB429C034E37EFD
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Received: from mx01.erols.com ([207.172.3.241]) by mta3.mail.erols.net
          (InterMail v03.02.07.03 118-128) with ESMTP
          id <19991026220405.KWQT2047@mx01.erols.com>
          for <vball@mta.mail.erols.net>; Tue, 26 Oct 1999 18:04:05 -0400
Received: from sundial.hujima (sundial.ma.huji.ac.il [132.64.32.5])
	by mx01.erols.com (8.8.8-970530/MX-980323-gjp) with ESMTP id SAA06153
	for <vball@erols.com>; Tue, 26 Oct 1999 18:04:00 -0400 (EDT)
Received: from localhost (moshez@localhost)
	by sundial.hujima (8.9.1b+Sun/8.9.3) with SMTP id AAA02803
	for <vball@erols.com>; Wed, 27 Oct 1999 00:01:38 +0200 (IST)
Date: Wed, 27 Oct 1999 00:01:38 +0200 (IST)
From: Moshe Zadka <moshez@math.huji.ac.il>
X-Sender: moshez@sundial
Reply-To: Moshe Zadka <mzadka@geocities.com>
To: Michael Perry <vball@erols.com>
Subject: Re: [Tutor] import calendar not working
In-Reply-To: <3815DFAD.AE2CBED9@erols.com>
Message-ID: <Pine.SOL.3.96.991026235940.2781A-100000@sundial>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
X-Mozilla-Status2: 00000000

On Tue, 26 Oct 1999, Michael Perry wrote:

> The filename is "cal.py"
> #!/usr/bin/python
> 
> print "Content-type: text/html" # HTML is following
> print                           # blank line, end of headers
> 
> import sys
> # import calendar
> 
> print sys.path
> # calendar.prmonth(1999, 10)
> 
> $ ./cal.py
> Content-type: text/html
> 
> ['.', '/usr/lib/python1.5/', '/usr/lib/python1.5/test',
> '/usr/lib/python1.5/plat-linux-i386', '/usr/lib/python1.5/lib-tk',
> '/usr/lib/python1.5/lib-dynload', '/usr/lib/python1.5/site-packages',
> '/usr/lib/python1.5/site-packages/HTMLgen',
> '/usr/lib/python1.5/site-packages/NumPy',
> '/usr/lib/python1.5/site-packages/PIL',
> '/usr/lib/python1.5/site-packages/idle']
> [mike@spin cgi-bin]$
> 
> 
> And this does not work?:
> 
> #!/usr/bin/python
> 
> print "Content-type: text/html" # HTML is following
> print                           # blank line, end of headers
> 
> import sys
> import calendar
> 
> print sys.path
> 
> 
> $./cal.py
> Content-type: text/html
> 
> Traceback (innermost last):
>   File "./cal.py", line 8, in ?
>     import calendar
>   File "/home/httpd/cgi-bin/calendar.py", line 15, in ?
>     import HTMLcalendar
>   File "/usr/lib/python1.5/site-packages/HTMLgen/HTMLcalendar.py", line
> 25, in ?
>     from calendar import day_name, month_name, mdays, weekday
> ImportError: cannot import name day_name

The trouble seem to be with some non-standard Python module "calendar".
It's hard to debug code you don't show us
--
Moshe Zadka <mzadka@geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.


--------------6029D8F58CB429C034E37EFD
Content-Type: text/plain; charset=us-ascii;
 name="calprob"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="calprob"

Script started on Tue Oct 26 19:12:39 1999
[httpd@spin cgi-bin]$ pwd
/home/httpd/cgi-bin
[httpd@spin cgi-bin]$ cp cal.py /tmp
[httpd@spin cgi-bin]$ cat cal.py
#!/usr/bin/python


print "Content-type: text/html" # HTML is following
print                           # blank line, end of headers

import calendar
import HTMLgen

calendar.prmonth(1999, 10)
[httpd@spin cgi-bin]$ ./cal.py
Content-type: text/html

Traceback (innermost last):
  File "./cal.py", line 7, in ?
    import calendar
  File "/home/httpd/cgi-bin/calendar.py", line 15, in ?
    import HTMLcalendar
  File "/usr/lib/python1.5/site-packages/HTMLgen/HTMLcalendar.py", line 25, in ?
    from calendar import day_name, month_name, mdays, weekday
ImportError: cannot import name day_name
[httpd@spin cgi-bin]$ /tmp/cal.py
Content-type: text/html

    October 1999     
Mo Tu We Th Fr Sa Su 
             1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 31 
[httpd@spin cgi-bin]$ exit

Script done on Tue Oct 26 19:13:27 1999

--------------6029D8F58CB429C034E37EFD
Content-Type: text/plain; charset=us-ascii;
 name="calendar.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="calendar.py"

###############################
# Calendar printing functions #
###############################

# Revision 2: uses funtions from built-in time module

# Import functions and variables from time module
from time import gmtime, localtime, mktime, asctime, ctime

# Exception raised for bad input (with string parameter for details)
error = 'calendar.error'

# Note when comparing these calendars to the ones printed by cal(1):
# My calendars have Monday as the first day of the week, and Sunday as
# the last!  (I believe this is the European convention.)

# Constants for months referenced later
January = 1
February = 2

# Number of days per month (except for February in leap years)
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Full and abbreviated names of weekdays
day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
	    'Friday', 'Saturday', 'Sunday']
day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

# Full and abbreviated names of months (1-based arrays!!!)
month_name =          ['', 'January',   'February', 'March',    'April', \
		           'May',       'June',     'July',     'August', \
			   'September', 'October',  'November', 'December']
month_abbr =       ['   ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
		           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

# Return 1 for leap years, 0 for non-leap years
def isleap(year):
	return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)

# Return number of leap years in range [y1, y2)
# Assume y1 <= y2 and no funny (non-leap century) years
def leapdays(y1, y2):
	return (y2+3)/4 - (y1+3)/4

# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
def weekday(year, month, day):
	secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
	tuple = localtime(secs)
	return tuple[6]

# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
def monthrange(year, month):
	if not 1 <= month <= 12: raise ValueError, 'bad month number'
	day1 = weekday(year, month, 1)
	ndays = mdays[month] + (month == February and isleap(year))
	return day1, ndays

# Return a matrix representing a month's calendar
# Each row represents a week; days outside this month are zero
def _monthcalendar(year, month):
	day1, ndays = monthrange(year, month)
	rows = []
	r7 = range(7)
	day = 1 - day1
	while day <= ndays:
		row = [0, 0, 0, 0, 0, 0, 0]
		for i in r7:
			if 1 <= day <= ndays: row[i] = day
			day = day + 1
		rows.append(row)
	return rows

# Caching interface to _monthcalendar
_mc_cache = {}
def monthcalendar(year, month):
	key = (year, month)
	if _mc_cache.has_key(key):
		return _mc_cache[key]
	else:
		_mc_cache[key] = ret = _monthcalendar(year, month)
		return ret

# Center a string in a field
def _center(str, width):
	n = width - len(str)
	if n <= 0: return str
	return ' '*((n+1)/2) + str + ' '*((n)/2)

# XXX The following code knows that print separates items with space!

# Print a single week (no newline)
def prweek(week, width):
	for day in week:
		if day == 0: s = ''
		else: s = `day`
		print _center(s, width),

# Return a header for a week
def weekheader(width):
	str = ''
	if width >= 9: names = day_name
	else: names = day_abbr
	for i in range(7):
		if str: str = str + ' '
		str = str + _center(names[i%7][:width], width)
	return str

# Print a month's calendar
def prmonth(year, month, w = 0, l = 0):
	w = max(2, w)
	l = max(1, l)
	print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1),
	print '\n'*l,
	print weekheader(w),
	print '\n'*l,
	for week in monthcalendar(year, month):
		prweek(week, w)
		print '\n'*l,

# Spacing of month columns
_colwidth = 7*3 - 1			# Amount printed by prweek()
_spacing = ' '*4			# Spaces between columns

# 3-column formatting for year calendars
def format3c(a, b, c):
	print _center(a, _colwidth),
	print _spacing,
	print _center(b, _colwidth),
	print _spacing,
	print _center(c, _colwidth)

# Print a year's calendar
def prcal(year):
	header = weekheader(2)
	format3c('', `year`, '')
	for q in range(January, January+12, 3):
		print
		format3c(month_name[q], month_name[q+1], month_name[q+2])
		format3c(header, header, header)
		data = []
		height = 0
		for month in range(q, q+3):
			cal = monthcalendar(year, month)
			if len(cal) > height: height = len(cal)
			data.append(cal)
		for i in range(height):
			for cal in data:
				if i >= len(cal):
					print ' '*_colwidth,
				else:
					prweek(cal[i], 2)
				print _spacing,
			print

--------------6029D8F58CB429C034E37EFD--