Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > import pickle succeeds only after two tries??

Reply
Thread Tools

import pickle succeeds only after two tries??

 
 
Bram Stolk
Guest
Posts: n/a
 
      09-23-2003
Hello,


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????

See this log:

Python 2.2.2 (#1, Mar 26 2003, 03:05:45)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/pickle.py", line 971, in ?
from StringIO import StringIO
ImportError: No module named StringIO
>>> import pickle
>>> dir(pickle)

['APPEND', 'APPENDS', 'BINFLOAT', 'BINGET', 'BININT', 'BININT1',
'BININT2', 'BINPERSID', 'BINPUT', 'BINSTRING', 'BINUNICODE', 'BUILD',
'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType',
'CodeType', 'ComplexType', 'DICT', 'DUP', 'DictProxyType', 'DictType',
'DictionaryType', 'EMPTY_DICT', 'EMPTY_LIST', 'EMPTY_TUPLE',
'EllipsisType', 'FLOAT', 'FileType', 'FloatType', 'FrameType',
'FunctionType', 'GET', 'GLOBAL', 'GeneratorType', 'INST', 'INT',
'InstanceType', 'IntType', 'LIST', 'LONG', 'LONG_BINGET', 'LONG_BINPUT',
'LambdaType', 'ListType', 'LongType', 'MARK', 'MethodType',
'ModuleType', 'NONE', 'NoneType', 'OBJ', 'ObjectType', 'PERSID', 'POP',
'POP_MARK', 'PUT', 'PickleError', 'Pickler', 'PicklingError',
'PyStringMap', 'REDUCE', 'SETITEM', 'SETITEMS', 'SHORT_BINSTRING',
'STOP', 'STRING', 'SliceType', 'StringType', 'StringTypes', 'TUPLE',
'TracebackType', 'TupleType', 'TypeType', 'UNICODE',
'UnboundMethodType', 'UnicodeType', 'Unpickler', 'UnpicklingError',
'XRangeType', '_EmptyClass', '_Stop', '__all__', '__builtins__',
'__doc__', '__file__', '__name__', '__version__', '_keep_alive',
'classmap', 'compatible_formats', 'dispatch_table', 'format_version',
'marshal', 'mdumps', 'mloads', 're', 'safe_constructors', 'struct',
'sys', 'whichmodule', 'x']

I find this extremely strange.
What could be going on here?

Thanks in advance,

Bram Stolk

 
Reply With Quote
 
 
 
 
Nick Welch
Guest
Posts: n/a
 
      09-23-2003
On Tue, Sep 23, 2003 at 10:59:03AM +0200, Bram Stolk wrote:
> The first 'import pickle' fails with "ImportError: No module named StringIO"
> If I immediately do a second 'import pickle', it works????
>

<snip>
>
> I find this extremely strange.
> What could be going on here?


Not sure about the exact problem you're having with pickle, however, the
import issue you are having is a general Python issue, not specific to
pickle or anything.

death@two ~ % echo 'print hello' > foo.py
death@two ~ % python
Python 2.3+ (#2, Sep 8 2003, 20:36:54)
[GCC 3.3.1 20030626 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "foo.py", line 1, in ?
print hello
NameError: name 'hello' is not defined
>>> import foo
>>>


--
Nick Welch aka mackstann | mack @ incise.org | http://incise.org
When you make your mark in the world, watch out for guys with erasers.
-- The Wall Street Journal

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-23-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Import sys' succeeds in C++ embedded code, but module is not fullyvisible Ben Sizer Python 9 02-10-2009 06:58 AM
pickle error: can't pickle instancemethod objects Michele Simionato Python 2 05-23-2008 08:29 AM
Using HttpResponse succeeds only after several tries redmond ASP .Net 0 12-06-2007 05:49 PM
redirect user after login succeeds Jon Paal ASP .Net 1 01-28-2007 12:56 AM
a pickle's pickle temposs@gmail.com Python 4 08-02-2005 07:20 PM



Advertisments