I'm aware of the standard example: if you execute a module directly then it's __name__
global variable is defined as "__main__"
. However, nowhere in the documentation can I find a precise description of how __name__
is defined in the general case. The module documentation says...
Within a module, the module's name (as a string) is available as the value of the global variable
__name__
.
...but what does it mean by "the module's name"? Is it just the name of the module (the filename with .py
removed), or does it include the fully-qualified package name as well?
How is the value of the __name__
variable in a Python module determined? For bonus points, indicate precisely where in the Python source code this operation is performed.
The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.
In Python, the special name __main__ is used for two important constructs: the name of the top-level environment of the program, which can be checked using the __name__ == '__main__' expression; and. the __main__.py file in Python packages.
The __module__ property is intended for retrieving the module where the function was defined, either to read the source code or sometimes to re-import it in a script.
__name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below.
However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name.
Python files are called modules and they are identified by the.py file extension. A module can define functions, classes, and variables. So when the interpreter runs a module, the __name__ variable will be set as __main__ if the module that is being run is the main program.
However, if you import a file as a module, Python sets the module name to the __name__ variable. First, create a new module called billing that has two functions: calculate_tax () and print_billing_doc (). In addition, add a statement that prints out the __name__ variable to the screen:
In the following code, you can see how a Python programmer uses the main function. Every Python module has its __name__ defined. If its value is __main__, that means the module is being run standalone by the user and the user can take the appropriate action for that main function. Thank you. Hope you like this article.
It is set to the absolute name of the module as imported. If you imported it as foo.bar
, then __name__
is set to 'foo.bar'
.
The name is determined in the import.c
module, but because that module handles various different types of imports (including zip imports, bytecode-only imports and extension modules) there are several code paths to trace through.
Normally, import
statements are translated to a call to __import__
, which is by default implemented as a call to PyImport_ImportModuleLevelObject
. See the __import__()
documentation to get a feel for what the arguments mean. Within PyImport_ImportModuleLevelObject
relative names are resolved, so you can chase down the name
variables there if you want to.
The rest of the module handles the actual imports, with PyImport_AddModuleObject
creating the actual namespace object and setting the name
key, but you can trace that name
value back to PyImport_ImportModuleLevelObject
. By creating a module object, it's __name__
value is set in the moduleobject.c
object constructor.
The __name__
variable is an attribute of the module that would be accessible by the name.
import os
assert os.__name__ == 'os'
Example self created module that scetches the import mechanism:
>>> import types
>>> m = types.ModuleType("name of module") # create new module with name
>>> exec "source_of_module = __name__" in m.__dict__ # execute source in module
>>> m.source_of_module
'name of module'
Lines from types module:
import sys
ModuleType = type(sys)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With