import pickle succeeds only after two tries??

Discussion in 'Python' started by Bram Stolk, Sep 23, 2003.

  1. Bram Stolk

    Bram Stolk Guest

    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
     
    Bram Stolk, Sep 23, 2003
    #1
    1. Advertising

  2. Bram Stolk

    Nick Welch Guest

    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
     
    Nick Welch, Sep 23, 2003
    #2
    1. Advertising

  3. Bram Stolk

    Peter Otten Guest

    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
     
    Peter Otten, Sep 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joergen Bech
    Replies:
    0
    Views:
    495
    Joergen Bech
    Jun 30, 2005
  2. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    393
  3. Jon   Paal

    redirect user after login succeeds

    Jon Paal, Jan 27, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    394
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 28, 2007
  4. redmond
    Replies:
    0
    Views:
    329
    redmond
    Dec 6, 2007
  5. Ben Sizer
    Replies:
    9
    Views:
    398
Loading...

Share This Page