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. Rice Jun 13 '13 at 14:14
-
2@KevinJ.Rice "the 'idiot' who picks up your code after you (i.e., you, 6 months later)" made my day! – Arctelix Nov 20 '14 at 18:30
-
20Who cares why he is asking the question? There are plenty of valid reasons to need to do this. – Christopher Barber Mar 16 '17 at 15:11
-
@Christopher: Although the need doesn't often arise, [here's](https://stackoverflow.com/a/66712690/355230) a case in point. – martineau Mar 19 '21 at 17:28
8 Answers
import sys
current_module = sys.modules[__name__]

- 26,247
- 4
- 39
- 46
-
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? – Dima Tisnek Jan 08 '13 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. – bukzor Oct 21 '13 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.

- 10,631
- 5
- 51
- 81
-
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? – JeremyDouglass Nov 24 '17 at 21:53
-
@JeremyDouglass. Not that I'm aware of. __import__ is a legit, documented, built-in function (the only __xx__ function). You can replace it with the 'importlib' package (you'll have to import it). Maybe - never happened to me - you could have an issue with relative/absolute import, if a module with the same name is available in sys.path, in which case you can solve it with the 'level' argument of the function. – Uri London Nov 28 '17 at 06:31
-
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 ? – sangorys Nov 20 '21 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 – blthayer Nov 22 '21 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. – Sargera May 11 '22 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 – odigity Aug 04 '22 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.

- 8,920
- 3
- 38
- 56
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

- 7,993
- 6
- 36
- 36
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

- 5,058
- 3
- 28
- 46
You can pass it in from outside:
mymod.init(mymod)
Not ideal but it works for my current use-case.

- 7,819
- 3
- 38
- 38
If all you need is to get access to module variable then use globals()['bzz']
(or vars()['bzz']
if it's module level).

- 543
- 1
- 6
- 12
from importlib import import_module
current_module = import_module(__name__)
PS: see also Sargera's comment

- 15,299
- 14
- 65
- 100