How to get a reference to a module inside the module itself?
Asked Answered
R

8

211

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?

Refund answered 4/11, 2009 at 21:42 Comment(4)
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.Toll
@KevinJ.Rice "the 'idiot' who picks up your code after you (i.e., you, 6 months later)" made my day!Standpipe
Who cares why he is asking the question? There are plenty of valid reasons to need to do this.Athanor
@Christopher: Although the need doesn't often arise, here's a case in point.Tales
N
263
import sys
current_module = sys.modules[__name__]
Nessus answered 4/11, 2009 at 21:45 Comment(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?Babushka
Reloading re-uses the same module object; no new module object is created, so it's still correct in the face of re-loading.Galangal
tks, only this answer works directly with module path with dotsShererd
F
30

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.

Finagle answered 28/7, 2017 at 17:0 Comment(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?Defector
@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.Finagle
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 ?Silveira
@Silveira - I too am seeing this behavior (getting a reference to the package instead of the module) on Python 3.9Pathology
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.Mccammon
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: https://mcmap.net/q/128799/-how-to-use-the-__import__-function-to-import-a-name-from-a-submoduleWhaleback
O
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.

Obie answered 4/11, 2009 at 21:46 Comment(0)
T
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

Tweeze answered 4/11, 2009 at 21:46 Comment(0)
E
5

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

Eubank answered 29/9, 2020 at 17:4 Comment(0)
C
3

You can pass it in from outside:

mymod.init(mymod)

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

Creeper answered 20/9, 2017 at 3:17 Comment(0)
C
1

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

Chickamauga answered 19/11, 2021 at 11:54 Comment(0)
A
0
from importlib import import_module
current_module = import_module(__name__)

PS: see also Sargera's comment

Ayotte answered 12/3, 2023 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.