Platform-dependent module search order?

John Machin sjmachin at lexicon.net
Sat Apr 28 17:54:38 EDT 2007


On Apr 29, 5:04 am, "Theo v. Werkhoven" <t... at van-
werkhoven.nl.invalid> wrote:
> The carbonbased lifeform John Machin inspired comp.lang.python with:
>
>
>
> > On Apr 28, 9:50 pm, "Theo v. Werkhoven" <t... at van-
> > werkhoven.nl.invalid> wrote:
> >> Goodday,
>
> >>             strg = array("B",octarray).tostring()
>
> > The above statement appears to be where the error manifests itself.
> > Possibilities: (1) array is bound to a list (2) the result of
> > array("B", octarray) has an attribute tostring which is bound to a
> > list. Option (1) seems less implausible. I'd be replacing that line
> > by:
>
> > print "octet %r, shift %r, array %r" % (octet, shift, array)
> > array_b = array("B", octarray)
> > print "array_b %r" % array_b
> > print "array_b.tostring %r" % array_b.tostring
> > strg = array_b.tostring()
>
> > You haven't shown us all of your code -- is array mentioned elsewhere?
> > What other imports are you doing? Do you have a file called array.py
> > in the offending directory? [I believe that this wouldn't matter,
> > because builtin modules like array can't be overridden by a file-based
> > module of the same name, but I could be wrong]
>
> Bingo!
> #v+
> theo:/home/theo/Devel/Python $ ls array*
> array.py  array.pyc
>
> theo:/home/theo/Devel/Python $ cat array.py
> #!/usr/bin/python
>
> import sys, os
>
> array = [14, 8765, 756, 5345, 98, 5634654, 234123, 9087, 58, 297, 7865]
> num = 0
>
> while 1:
>         try:
>                 print num
>                 num = num + array.pop()
>         except:
>                  break
> #v-
> And that code produces the numbers I was seeing..
>
> > What platform and what version of Python?
>
> $ python --version
> Python 2.5
>
> $ uname -srv
> Linux 2.6.18.8-0.1-default #1 SMP Fri Mar 2 13:51:59 UTC 2007

Very interesting. My first reaction to Theo's posting was to make a
confident declaration like Carsten did, but I couldn't simulate this
behaviour on Windows with Python 2.5.1 (or 2.1.3 for that matter) and
moreover the docs say:
"""
Details of the module searching and loading process are implementation
and platform specific. It generally involves searching for a ``built-
in'' module with the given name and then searching a list of locations
given as sys.path.
"""
(from http://docs.python.org/ref/import.html)

I'm now curious about the rationale for having/permitting such a
difference.

Here's what I get on Windows XP Pro SP2 with Python 2.5.1:
8<-------------------------------------------
C:\junk>type array.py
print "Gotcha!"

C:\junk>type arrayimport.py
import sys, pprint
print 'sys.path:'
pprint.pprint(sys.path)
import array
print 'array: %r' % array
C:\junk>\python25\python arrayimport.py
sys.path:
['C:\\junk',
 'C:\\python25\\lib\\site-packages\\cheesecake-0.6.1-py2.5.egg',
 'C:\\python25\\lib\\site-packages\\setuptools-0.6c5-py2.5.egg',
 'C:\\python25\\python25.zip',
 'C:\\python25\\DLLs',
 'C:\\python25\\lib',
 'C:\\python25\\lib\\plat-win',
 'C:\\python25\\lib\\lib-tk',
 'C:\\python25',
 'C:\\python25\\lib\\site-packages']
array: <module 'array' (built-in)>
8<----------------------------------------------------------------------




More information about the Python-list mailing list