Javatpoint Logo
Javatpoint Logo

__name__ in Python

The name of the Python script or module currently executing is stored in Python's __name__ special variable. Python 3.0 introduced the __name__ variable, which is absent from Python 2. x. The current Python script or module's value, __main__, is assigned to the __name__ variable while it is being run.

What does _name_ mean ?

Python has a built-in variable called __name__ that stores the name of the script or module that is currently executing. The script or module name is stored in the __name__ variable unless the current module is running, in which case the value __main__ is assigned to it.

Consequently, when a Python script is being executed, the value of its __name__ variable should always be __main__ if it is imported into another Python script. If not, the module's name would appear.

Example :

To further understand this, let's use an example. Make a script in Python called testing.py, and append the following code to it :

Output:

Value of the __name__ :  __main__

Explanation:

The script is running immediately as the main program, as indicated by the output "Value of the name: main." In Python, the value of the script's __name__ variable is automatically set to '__main__' when the script is executed directly. This output confirms that the script is being run directly rather than imported as a module into another script

Let's now build another Python script called mains.py and import the previous one into it.

Example :

Output:

Value of the __name__ : testing

Explanation:

testing. anything() is the method that is run when the script testing.py is imported into another Python script using import testing. In this situation, the name of the module, in this case "testing," is set as the value of the __name__ variable in testing.py rather than "__main__." For this reason, the output of testing. anything() is 'Value of the __name__: testing.' This illustrates how running the script directly vs importing it as a module affects the value of the __name__ variable.

Using the condition if name == main :

We use the if statement and the condition __name__ == __main__ to declare that certain Python code should only be performed when the script is run directly.

Example :

In this case, the current module or script's ability to run independently is ascertained using the string __main__. To indicate to the Python interpreter that a keyword in the __name__ variable is reserved or special, there are two underscores on either side of the term.

Code example for name in Python :

As said before, when we run a code file, the code is performed directly-it isn't even imported into another file-and as a result, the value of the __name__ variable changes to __main__.

Output:

It is a function in the ScriptP1.
Called from the ScriptP1.

Explanation:

The function anything() specified in the script ScriptP1 is being run directly as the main program, as shown by the output "It is a function in the ScriptP1." followed by "Called from the ScriptP1." When the script is run directly in Python, the __name__ variable is set to '__main__'; consequently, the condition if __name__=='__main__': evaluates to True, calling the function anything(). The print statement that follows verifies that the script is indeed being run directly.

Now, let's make a new Python script file named ScriptP2.py, import ScriptP1.py into it, and try to call the ScriptP1 method anything().

Code : The ScriptP2.py code is provided here:

Output:

ScriptP1 is imported into another file.
It is a function in the ScriptP1.
Called from the ScriptP2.

Explanation:

The provided Python script shows how to import the "ScriptP1" module into the active script. The method "anything()" is present in "ScriptP1," and it's called in the main script. This function is called, and the script outputs 'Called from the ScriptP2.' to indicate that the function call has been completed. This flow of events indicates that the function specified in "ScriptP1" was accessible and successfully performed in the context of the script that is now running, which is identified as "ScriptP2".

When the import statement for ScriptP1 was run inside of ScriptP2, the __name__ variable had the value ScriptP1 (the module name); but, since ScriptP2 was the first script to run, it will now have the value __main__.

Printing the Value of __name__ :

Let's print the value of the __name__ variable at each stage of execution to help you understand it better.

Example : The ScriptP1.py Python script's source code is provided below :

Output:

Value or the variable __name__ : __main__

Explanation:

The sample of Python code that is supplied outputs the value of the variable __name__, which, when the script is run directly, evaluates to "main." It means that when the script is launched directly, the code block that comes after the if __name__ == '__main__': condition will run rather than being imported as a module into another script. This kind of behavior is frequently used to distinguish between executing a script as the primary application and importing it into another script as a module.

Example 2 : And here is the script ScriptP2.py's source code :

Output:

Value or the variable __name__ : __main__

Explanation:

The provided Python script uses the import statement to import the file "ScriptP1.py. " When it is executed, "main" is printed as the value of the variable __name__. This means that instead of being imported as a module into another script, the script is being executed directly as the primary application. When executing Python scripts, this is the usual behavior; the script's execution context is reflected in the __name__ variable.

Summary :

In most programming languages, the main method or function is usually utilized as the point at which any program is performed. But what about Python? Execution of a Python program (script) normally starts at line zero or the program's indentation level 0. The creation of a __name__ variable, however, happens before a Python program runs. This variable might be used to replace the main method in Python.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA