Skip to content Skip to sidebar Skip to footer

How Do I Get The Module Instance For A Class In Python?

I am learning Python, and as always, I'm being ambitious with my starter projects. I am working on a plugin system for a community site toolkit for App Engine. My plugin supercla

Solution 1:

The sys.modules dict contains all imported modules, so you can use:

mod = sys.modules[__module__]

Solution 2:

How about Importing modules

X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.

You could pass the module name (instead of 'X') and get the module instance back. Python will ensure that you don't import the same module twice, so you should get back the instance that you imported earlier.

Solution 3:

Alternatively, you can use the module's global __file__ variable if all you want is the module's path.

Post a Comment for "How Do I Get The Module Instance For A Class In Python?"