[Tutor] function won't import from module

Dick Moores rdm at rcblue.com
Tue Aug 9 06:41:37 CEST 2005


I have a bunch of functions I've collected in one script, "mycalc.py", 
which I use as a module, "mycalc". The last one I wrote, cmpSeq() is as 
follows:

===========begin code==============
def cmpSeq(seq1, seq2):
     """
     find first index at which two sequences differ
     """
     if seq1 == seq2:
         print "Sequences are identical, and of length %d" % len(seq1)
         return None
     if len(seq1) >= len(seq2):
         shorterOrEqualSequence = seq2
     else:
         shorterOrEqualSequence = seq1

     for index in range(len(shorterOrEqualSequence)):
         if seq1[index] != seq2[index]:
             print "sequences first differ at index", index
             print "seq1[%d] = %s" % (index, seq1[index])
             print "seq2[%d] = %s" % (index, seq2[index])
             break

     if index == len(shorterOrEqualSequence)-1:
         print "sequences are identical thru end of shorter sequence at 
index", index

     print "len(seq1) =", len(seq1)
     print "len(seq2) =", len(seq2)
========end code for cmpSeq()=========

In a script, cmpSeq() works fine. For example followed by

a = "qwertys"
b = "qxerty"
cmpSeq(a,b)

The output is:

sequences first differ at index 1
seq1[1] = w
seq2[1] = x
len(seq1) = 7
len(seq2) = 6



cmpSeq() is now copy-pasted into mycalc.py, but is not useable there:

#testof_cmpSeq.py
import mycalc
a = "qwerty"
b = "qxerty"
mycalc.cmpSeq(a,b)

which produces:

Traceback (most recent call last):
   File "C:\Python24\MyScripts\testof_cmpSeq.py", line 1, in -toplevel-
     from mycalc import cmpSeq
ImportError: cannot import name cmpSeq

However, other functions in mycalc work fine. For example:

#testof_print_hms
import mycalc

seconds = 87658
mycalc.print_hms(seconds)

This outputs   24 hours, 20 minutes, 58 seconds



That may have been a bit long-winded; if so, I apologize.

But would someone please point out why cmpSeq() can't be imported from 
mycalc?

Thanks,

Dick Moores












More information about the Tutor mailing list