187

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?

martineau
  • 112,593
  • 23
  • 157
  • 280
Ram Rachum
  • 77,567
  • 79
  • 223
  • 360
  • 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. – 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
  • 17
    Who 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

7 Answers7

242
import sys
current_module = sys.modules[__name__]
mthurlin
  • 25,019
  • 4
  • 37
  • 46
  • 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? – Dima Tisnek Jan 08 '13 at 22:05
  • 16
    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 '13 at 22:26
27

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.

Uri
  • 10,347
  • 5
  • 50
  • 79
  • 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 Nov 28 '17 at 06:31
  • 2
    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 '21 at 15:59
  • 1
    @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
  • 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 '22 at 11:21
16

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.

Michael
  • 8,624
  • 3
  • 36
  • 54
13

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

pkit
  • 7,613
  • 6
  • 33
  • 36
3

You can pass it in from outside:

mymod.init(mymod)

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

Sam Watkins
  • 7,242
  • 3
  • 37
  • 38
3

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

Brandt
  • 3,920
  • 3
  • 25
  • 38
0

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

Karolius
  • 433
  • 4
  • 12