import pickle succeeds only after two tries??

Peter Otten __peter__ at web.de
Tue Sep 23 06:25:28 EDT 2003


Bram Stolk wrote:

> I am using python-2.2.2 on linux-ARM.
> Python itself works OK.
> However, importing pickle gives me *very* strange results:
> 
> The first 'import pickle' fails with "ImportError: No module named
> StringIO" If I immediately do a second 'import pickle', it works????

The first import puts the module into sys.modules, then executes it.
I execution succeds, the module object is assigned to a variable in the
current global namespace, if it fails, no such assignment is performed.

The second import tries to find the module in sys.modules, finds it and
therefore does not execute it again.

The effects are strange, for example:

--- test.py ---
def alpha():
    pass

print unknown

def beta():
    pass
--- end test.py ---

>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "test.py", line 5, in ?
    print unknown
NameError: name 'unknown' is not defined
>>> import test
>>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', 'alpha']
>>>

Note that beta() is missing while alpha() is there. I doubt that this
behaviour is intentional.

Peter






More information about the Python-list mailing list