How to debug this import problem?

Scott David Daniels Scott.Daniels at Acm.Org
Fri May 8 14:21:24 EDT 2009


Diez B. Roggisch wrote:
> Iwan Vosloo wrote:
>> We have a rather complicated program which does a bit of os.chdir and
>> sys.path manipulations.  In between all of this, it imports the decimal
>> module several times....
>> However, it imports a new instance of decimal sometimes.  (Which is a
>> problem, since a decimal.Decimal (imported at point A) now is not the
>> same as that imported somewhere else....
>>
>> In trying to figure out what's happening, we've changed the code in
>> decimal to print out id(sys) when decimal gets imported.  This also
>> gives back different values at different times.  My suspicion is that
>> when importing python tries to check sys.modules - but each time sys is
>> a different sys already.
>>
>> Any ideas of how we can gather more data to find out exactly what causes
>> this? The path manipulations is a suspect, but unfortunately we cannot
>> remove it.  If we can trace the problem from the import end though, we
>> may be able to isolate the exact one which is the problem.
>>
>> This code incidentally also runs in a virtualenv environment AND uses
>> setuptools.  None of these complications can be removed...
> 
> Try putting an "import pdb; pdb.set_trace()" on top of the decimal module.
> Then you can see whenever it gets imported new, and with "bt" get a
> backtrace, and you can inspect (and thus eventually compare) the various
> states of sys.path and sys.modules.


You can also run Python with the "-v" or "-vv" flags to get output
about exactly what files are getting imported from the file
system and when.  "-v" iswhat it got, "-vv" is everywhere it tries.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list