Trouble importing / using a custom text codec
Jurie Horneman
jhorneman at pobox.com
Thu Feb 28 04:52:09 EST 2002
I'm having an irritating problem with a text codec I've written.
- The codec is in site-packages/MBCS_codecs/cp932.py.
- site-packages/MBCS_codecs contains an empty __init__.py.
- from MBCS_codecs import cp932 works fine.
- If I put cp932.py in the same place as a script that decodes a text
using encoding "cp932", there is no problem.
However, I want to have all my encodings somewhere else...
- With cp932.py in site-packages as described above, if I decode a
text using encoding "MBCS_codecs.cp932", I get the error message
"LookupError: unknown encoding". This happens even if I import cp932
by hand before decoding the text.
- The code that fails is in encodings/__init__.py :
modname = encoding.replace('-', '_')
modname = aliases.aliases.get(modname,modname)
try:
mod = __import__(modname,globals(),locals(),'*')
except ImportError,why:
# cache misses
_cache[encoding] = None
return None
- Executing the following code in one of my own scripts works:
modname = "MBCS_codecs.cp932"
modname = modname.replace('-', '_')
try:
mod = __import__(modname,globals(),locals(),'*')
except ImportError,why:
print "FAILED"
i.e. the module is imported.
- The aliases in encoding/__init__.py do not modify the module name.
In other words, it appears that the same code works or does not work
depending on where it is executed.
I cannot explain this behavior. Any help is appreciated.
Jurie Horneman
More information about the Python-list
mailing list