[Paul Grasso wrote]
> I want to trace an import statement, but only one so I set
> 
> import os
> os.environ["PYTHONVERBOSE"] = "1"
> import wx
> del os.environ["PYTHONVERBOSE"]
> 
> but the import wx was NOT traced. Neither was it traced if I use os.putenv. 
> Does anyone know how to set python for import tracing within a script? 

You cannot use PYTHONVERBOSE for this because Python only reads that
environment variable in its startup code. It is either on for everything
or off.

You'd best be able to do what you want by setting up an "import hook". Here is
an example of something you might want:

    import os

    def verbose_import_func(name, globals=None, locals=None, fromlist=None):
        global old_import_func
        module = old_import_func(name, globals, locals, fromlist)
        print "import %s # %s" % (name, module.__file__)
        return module

    def setup_import_hook():
        global old_import_func
        old_import_func = __import__
        __builtins__.__import__ = verbose_import_func

    def teardown_import_hook():
        __builtins__.__import__ = old_import_func

    import cmd
    setup_import_hook()
    import xml
    teardown_import_hook()


I couldn't quickly find very good documentation on Python import hooks, but
here is a start (describing changes to the import hook system in Python 2.3):

    http://www.python.org/doc/2.3.5/whatsnew/section-pep302.html


Cheers,
Trent

-- 
Trent Mick
[EMAIL PROTECTED]
_______________________________________________
ActivePython mailing list
ActivePython@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython

Reply via email to