How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?
-
7I suspect you might be asking this question because you have a variable in module scope (e.g., BLAH=10 outside a function or class), then a class/function variable named BLAH, and you want to differentiate. A valid question here is: Is this necessary? Scope rules are notoriously prone to mistake, especially by the 'idiot' who picks up your code after you (i.e., you, 6 months later). Tricks like this are rarely necessary; I attempt to avoid them completely because they're so often confusing and wrongly modified later.– Kevin J. RiceJun 13, 2013 at 14:14
-
2@KevinJ.Rice "the 'idiot' who picks up your code after you (i.e., you, 6 months later)" made my day!– ArctelixNov 20, 2014 at 18:30
-
20Who cares why he is asking the question? There are plenty of valid reasons to need to do this.– Christopher BarberMar 16, 2017 at 15:11
-
@Christopher: Although the need doesn't often arise, here's a case in point.– martineauMar 19, 2021 at 17:28
8 Answers
import sys
current_module = sys.modules[__name__]
-
3except for this won't be quite correct if module is reloaded; I don't think there's any place where a reference is guaranteed to be kept, if there was, reloading wouldn't really work, right? Jan 8, 2013 at 22:05
-
19Reloading re-uses the same module object; no new module object is created, so it's still correct in the face of re-loading.– bukzorOct 21, 2013 at 22:26
-
One more technique, which doesn't import the sys module, and arguably - depends on your taste - simpler:
current_module = __import__(__name__)
Be aware there is no import. Python imports each module only once.
-
This seems like a really nice way to avoid importing sys. Other than being a bit counter-intuitive to read are there any potential downsides to this approach? Nov 24, 2017 at 21:53
-
4This solution does not work on my code. Instead of create a reference to the module, it creates a reference to the package. Am I the only one to have this behavior ?– sangorysNov 20, 2021 at 15:59
-
3@sangorys - I too am seeing this behavior (getting a reference to the package instead of the module) on Python 3.9– blthayerNov 22, 2021 at 17:41
-
4Per the docstring: "When importing a module from a package, note that __import__('A.B', ...) returns package A when fromlist is empty, but its submodule B when fromlist is not empty." It is also advised in the same place to use importlib.import_module instead, which does not have this behavior.– SargeraMay 11, 2022 at 11:21
-
2Do not use this answer, it will drive you nuts with unintuitive behavior like sometimes importing a higher up module than the one you specified. Details: stackoverflow.com/a/37308413/210867– odigityAug 4, 2022 at 15:36
If you have a class in that module, then the __module__
property of the class is the module name of the class. Thus you can access the module via sys.modules[klass.__module__]
. This is also works for functions.
You can get the name of the current module using __name__
The module reference can be found in the sys.modules
dictionary.
See the Python documentation
According to @truppo's answer and this answer (and PEP366):
Reference to "this" module:
import sys
this_mod = sys.modules[__name__]
Reference to "this" package:
import sys
this_pkg = sys.modules[__package__]
__package__
and__name__
are the same if from a (top)__init__.py
You can pass it in from outside:
mymod.init(mymod)
Not ideal but it works for my current use-case.
If all you need is to get access to module variable then use globals()['bzz']
(or vars()['bzz']
if it's module level).
from importlib import import_module
current_module = import_module(__name__)
PS: see also Sargera's comment