Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

__file__ (A Special variable) in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A double underscore variable in Python is usually referred to as a dunder. A dunder variable is a variable that Python has defined so that it can use it in a “Special way”. This Special way depends on the variable that is being used.

Note: For more information, refer to Dunder or magic methods in Python

The __file__ variable:

__file__ is a variable that contains the path to the module that is currently being imported. Python creates a __file__ variable for itself when it is about to import a module. The updating and maintaining of this variable is the responsibility of the import system. The import system can choose to leave the variable empty when there is no semantic meaning, that is when the module/file is imported from the database. This attribute is a String. This can be used to know the path of the module you are using. To understand the usage of __file__ consider the following example.

Example: Let’s create a module named HelloWorld and store it as a .py file.


# Creating a module named
# HelloWorld
def hello():
    print("This is imported from HelloWorld")
# This code is improved by gherson283

Now let’s create another file named that imports the above-created module to show the use of __file__ variable.


# Importing the above
# created module
import HelloWorld
# Calling the method
# created inside the module
# printing the __file__
# variable




My Personal Notes arrow_drop_up
Last Updated : 08 Aug, 2021
Like Article
Save Article
Similar Reads
Related Tutorials