What does if __name__ == “__main__”: do?

What does if __name__ == “__main__”: do?

If you have started working with python, then in python files you must have seen the following code as the last lines

if __name__ == "__main__":
    main()

Being a NodeJS/Angular developer, I was confused about this line. So here is the explanation

  1. import a action actually runs all that can be ran in a.py, meaning each line in a.py
  2. Because of point 1, you may not want everything to be run in a.py when importing it
  3. To solve the problem in point 2, python allows you to put a condition check
  4. __name__ is an implicit variable in all .py modules:
  • when a.py is imported, the value of __name__ of a.py module is set to its file name "a"
  • when a.py is run directly using "python a.py", the value of __name__ is set to a string __main__

5. Based on the mechanism how python sets the variable __name__ for each module, do you know how to achieve point 3? The answer is fairly easy, right? Put a if condition: if __name__ == "__main__": // do A

  • then python a.py will run the part // do A
  • and import a will skip the part // do A

6. You can even put if __name__ == "a" depending on your functional need.

Reference

https://stackoverflow.com/questions/419163/what-does-if-name-main-do#:~:text=In%20short%2C%20use%20this%20'%20if,when%20the%20module%20is%20imported.&text=Put%20simply%2C%20__name__,run%20as%20an%20imported%20module.

To view or add a comment, sign in

More articles by Sunil Garg

  • Jupyter

    Kind of IDE best for data analysis Open source web application, allows to create and share live code, equations and…

  • AI vs ML vs DL vs DS

    AI - Artificial Intelligence Enables the machine to think Machine can take own decisions AI application can be final…

  • Python Pandas - Data Analysis

    Open source data analysis library written in python Leverages the power and speed of numpy to make data analysis and…

  • Compounds in launch.json : Debugging multiple projects VSCode

    If you want to debug multiple projects in VSCode, then you can use compounds to run multiple configuration. Adding…

  • JavaScript : V8 engine

    V8 is the name of the JavaScript engine that powers Google Chrome. It's the thing that takes our JavaScript and…

  • Kubernetes : Architecture and Basic Idea

    Orchestration Automated tool to manage group of micro-services Kubernetes Orchestration tool in the container landscape…

    2 Comments
  • Docker Compose

    Docker Compose To run multiple containers as single service Docker-compose command will be available run the containers…

  • Docker File and Repositories

    Docker File A file with instructions to build your own docker image Name should be Dockerfile Docker File Instruction…

  • Docker - Basic Idea and Important Commands

    Docker Container based technology and containers are just user space of the operating system Open platform for…

  • Python - Virtual Environment

    Virtual Environment (venv) in python Python environment such that the Python interpreter, libraries and scripts…

Explore content categories