Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display details of importer

Tags:

python

import

In Django I have a package that issues a depreciation warning (django.views.generic.simple). It would be useful if this warning described where the import was being made from, so the coder can go in and change the file without having to step through code to find it.

So the general case is

#file1.py
import file2.py

#file2.py
import warnings
warnings.warn(
'Package deprecated: imported from %s' % __importer__,
DeprecationWarning
)

Where __importer__ is an imaginary attribute containing "file1.py", or some such reference.

Is there a way to do this?

like image 549
powlo Avatar asked Aug 17 '12 14:08

powlo


1 Answers

Yes, this is done by using the stacklevel argument to warnings.warn. See the example in the documentation for more info.

like image 166
Julian Avatar answered Sep 27 '22 23:09

Julian