The __file__ is a special variable in Python that contains the module's path. In Python, we use modules to perform various tasks and solve complex problems. At the beginning of programs, we need to install modules and packages to use the functions. So, this module has its path where it is stored.
A Python programme uses the condition if __name__ == '__main__' to only run the code inside the if statement when the program is run directly by the Python interpreter. The code inside the if statement is not executed when the file's code is imported as a module.
What is the purpose of __name__ ? Before executing a program, the Python interpreter assigns the name of the python module into a special variable called __name__ . Depending on whether you are executing the program through command line or importing the module into another module, the assignment for __name__ will vary.
__FILE__ is a preprocessor macro that expands to full path to the current file. __FILE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
__FILE__ and __LINE__ in Python
In C++, if you want your code to talk about itself, you often use the predefined magic macros __FILE__ and __LINE__ to get the filename and line number of the current line: // Use this macro if you can't write the code yet.
Get the path of current file (script) in Python: __file__ In Python, you can use __file__ to get the path (location) of the current file, i.e., the currently running script file ( . py ). This is particularly useful when you need to read other files that are relative to the location of the current file.
__name__ is the special variable in python that provides the name of the current module where it is used. Note that to display the class name you have to create the object for that lass and display its name using class.name.
__name__ is the name of the current Python module. The app needs to know where it's located to set up some paths, and __name__ is a convenient way to tell it that. instance_relative_config=True tells the app that configuration files are relative to the instance folder.
__name__ variable in Python
Since there is no main() function in Python, when a module(file) is executed, Python interpreter creates one special global variable __name__. By default, its value is set to __main__.
__init__.py , among other things, labels a directory as a python directory and lets you set variables on a package wide level. __main__.py , among other things, is run if you try to run a compressed group of python files. __main__.py allows you to execute packages.
To import a file from a different folder in Python, you will need to use the sys. path. append() function to add the path to the folder containing the file to the Python path. The Python path is a list of directories that Python searches for modules and packages.
The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard, and that's it.
The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
The python __init__ method is declared within a class and is used to initialize the attributes of an object as soon as the object is formed. While giving the definition for an __init__(self) method, a default parameter, named 'self' is always passed in its argument. This self represents the object of the class itself.
The __path__ attribute of a Python package is used by the import system when trying to import modules of subpackages. It is a list (or at least an iterable) of paths that can be used when importing modules or subpackages.
__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.
Using the python Command
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.
The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.