[Python-Dev] Demo/embed causes 'import site' failure

Guido van Rossum guido@beopen.com
Mon, 10 Jul 2000 11:30:57 -0500


(There's a linux question at the bottom.)

> > I decided to build the simple embedding demo in Demo/embed/.  After
> > fixing the Makefile (checkins coming), I ended up with one problem:
> > the 'import site.py' failed error.  When using -v I get this traceback:
> > 
> > 'import site' failed; traceback:
> > Traceback (most recent call last):
> >   File "/usr/local/lib/python2.0/site.py", line 146, in ?
> >     sys.setdefaultencoding('ascii')
> > LookupError: no codec search functions registered: can't find encoding
> > 
> > Is there some part of the initialization that I'm forgetting?
> 
> Looks like the 'import encodings' failed. Could you check this
> with -vv ?

Thanks for the hint.  Seems I've got it nailed down: it's looking for
the struct module:

[...]
import codecs # precompiled from /usr/local/lib/python2.0/codecs.pyc
# trying /usr/local/lib/python2.0/struct.so
# trying /usr/local/lib/python2.0/structmodule.so
# trying /usr/local/lib/python2.0/struct.py
# trying /usr/local/lib/python2.0/struct.pyc
# trying /usr/local/lib/python2.0/plat-linux2/struct.so
# trying /usr/local/lib/python2.0/plat-linux2/structmodule.so
# trying /usr/local/lib/python2.0/plat-linux2/struct.py
# trying /usr/local/lib/python2.0/plat-linux2/struct.pyc
# trying /usr/local/lib/python2.0/lib-tk/struct.so
# trying /usr/local/lib/python2.0/lib-tk/structmodule.so
# trying /usr/local/lib/python2.0/lib-tk/struct.py
# trying /usr/local/lib/python2.0/lib-tk/struct.pyc
# trying /usr/local/lib/python2.0/lib-dynload/struct.so
# trying /usr/local/lib/python2.0/lib-dynload/structmodule.so
'import site' failed; traceback:
Traceback (most recent call last):
  File "/usr/local/lib/python2.0/site.py", line 146, in ?
    sys.setdefaultencoding('ascii')
LookupError: no codec search functions registered: can't find encoding
[...]

But the bizarre thing is that the last file it tries
(/usr/local/lib/python2.0/lib-dynload/structmodule.so) actually
exists!!!

OK, so there's code masking the ImportError in import_encodings() in
codecs.c.  I disabled that (hmm, maybe this should be disabled
automatically when -v is used!).  Now I get this traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python2.0/site.py", line 146, in ?
    sys.setdefaultencoding('ascii')
  File "/usr/local/lib/python2.0/encodings/__init__.py", line 30, in ?
    import codecs,aliases
  File "/usr/local/lib/python2.0/codecs.py", line 10, in ?
    import struct,types,__builtin__
ImportError: /usr/local/lib/python2.0/lib-dynload/structmodule.so: undefined symbol: PyString_Type

OK, I've managed to fix it by a change to the way I build Python: I'm
now linking the struct module (and everything in Setup up to and
including _codecs) statically.

But I guess what's really missing is some kind of flag that tells the
linker to export all symbols in the libpython2.0.a file...  Is there a
way?  (I feel so stupid!)

--Guido van Rossum (home page: http://dinsdale.python.org/~guido/)