 <p>As I was writing the question, I figured out the answer, so I decided to post it right here. But if someone has a different solution, that is more stable or clean, I'm still interested.</p> <p>The dictionary to be passed to <code>.format</code> should be:</p> <p><code>vars(sys.modules[func.__module__])</code></p>