Background: Suppose in a big python application your main script imports a few packages and modules. One of them is mod2.py, which in turn imports mod2a.py.
Now You need to add invesgative logging/instrumentation to mod2a.py but this file is loaded from a readonly firm-wide repository, common practice in big teams. Here’s my tested technique:
1. clone mod2a.py to your home dir and add the logging. Now we need to import this modified version.
2. clone mod2.py to your home dir and open it to locate the importation of mod2a
3. edit mod2.py to change the importation of mod2a:
import mod2a # via /home/dir
All other imports should be unaffected.
4. edit main.py and update the importation of mod2.py to load it too from /home/dir
As an additional hack, Some people may rename the modified mod2a.py file. This is doable IIF the import line is
from mod2a import someSymbol
Otherwise, every mention of “mod2a” in mod2.py needs a change