469,323 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

import pickle succeeds only after two tries??

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

Jul 18 '05 #1
2 2291
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

Jul 18 '05 #2
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by David McNab | last post: by
9 posts views Thread by Larry goodman | last post: by
16 posts views Thread by Manlio Perillo | last post: by
3 posts views Thread by Christoph Zwerschke | last post: by
1 post views Thread by Nick Coghlan | last post: by
2 posts views Thread by praveenkumar.117 | last post: by
1 post views Thread by Nick Craig-Wood | last post: by
reply views Thread by =?Utf-8?B?cmVkbW9uZA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.