Monday, 5 January 2026

Python Internal Modules - The Underscore Convention

After debugging Python for a while you will certainly see some files with leading underscores. 

This is a Python convention to indicate the file is not part of the public interface of the module.  An example could be _mixins.py for a mixins class (this is used in pandas for example).  

When doing a "from package import *" any file with an underscore is not imported, respecting this convention. 

Sometimes an underscore can also be used to prevent name collisions e.g. implementing some JSON helpers in _json.py to avoid conflict with another json.py (pandas has this file too).

Pat yourself on the back if you have debugged into an underscore-prefixed file - you have ventured into the hidden depths of a package's implementation details. Good Job!

No comments: