203

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?

4
  • 7
    I 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. Jun 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!
    – Arctelix
    Nov 20, 2014 at 18:30
  • 20
    Who cares why he is asking the question? There are plenty of valid reasons to need to do this. Mar 16, 2017 at 15:11
  • @Christopher: Although the need doesn't often arise, here's a case in point.
    – martineau
    Mar 19, 2021 at 17:28

8 Answers 8

254
import sys
current_module = sys.modules[__name__]
3
  • 3
    except 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
  • 19
    Reloading 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, 2013 at 22:26
  • tks, only this answer works directly with module path with dots
    – Dee
    Dec 18, 2022 at 21:50
29

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.

6
  • 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
  • 4
    This 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, 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
    – blthayer
    Nov 22, 2021 at 17:41
  • 4
    Per 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, 2022 at 11:21
  • 2
    Do 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 4, 2022 at 15:36
17

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.

0
15

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

0
4

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

3

You can pass it in from outside:

mymod.init(mymod)

Not ideal but it works for my current use-case.

0
0

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

0
from importlib import import_module
current_module = import_module(__name__)

PS: see also Sargera's comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.