Python Module Internal Representation and Introspection: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 11: Line 11:
=Attributes=
=Attributes=
==<tt>__file__</tt>==
==<tt>__file__</tt>==
Once imported, the file associated with the module can be determined using the module object's __file__ attribute:
<syntaxhighlight lang='python'>
import mymodule
[...]
print(mymodule.__file__)
</syntaxhighlight>
The directory portion of <code>__file__</code> should be one of the directories in <code>[[#sys.path|sys.path]]</code>.

Revision as of 01:54, 4 January 2023

Internal

Overview

The module Class

All module instances are represented internally as instances of the module

Attributes

__file__

Once imported, the file associated with the module can be determined using the module object's __file__ attribute:

import mymodule
[...]
print(mymodule.__file__)

The directory portion of __file__ should be one of the directories in sys.path.