Error with co_filename when loading modules from zip file

Bob bob at
Mon Mar 5 21:36:39 CET 2012


I'm using a program that distributes python in a zip
file and ran into an issue with the logging package.
It seems to return the wrong filename/line number when
loading python from a zip file. Please help!

I'm using python31, and have copied the lib directory to
and have created a zip of that directory and placed it in 

The logging package gets the filename and line number
of the calling function by looking at two variables, the filename
of the frame in the stack trace and the variable logging._srcfile.
The comparison is done in logging/

In the situation above, when I run,
  PYTHONPATH=/home/user/python3.1 ./myexe
I see 
Here, filename and _srcfile are the same, so the logger correctly
outputs the filename of

When I run,
  PYTHONPATH=/home/user/python3.1/ ./myexe
I see 
Here, filename and _srcfile are different, so the logger incorrectly
outputs the filename of /home/user/python3.1/logging/

I've noticed this:
  - the filename seems to be set when you compile the module
  - it seems to be set when you load the module (even after moving it)
  - it does not seem to get set when you load the module from
    the pyc in the zip file!

I've tried putting only the pyc files, only the py files
and both in the zip file.

Any help?

import logging

class Handler(logging.Handler):
    def __init__(self):

    def emit(self, record):
        print('message: ' + record.msg)
        print('filename: ' + record.pathname)
        print('line: ' + str(record.lineno))


More information about the Python-list mailing list