[python] __name__ == __main__
__name__ : 현재 실행중인 인스턴스의 이름을 나타내는 파이썬의 내장함수
https://docs.python.org/3/library/__main__.html
__main__ — Top-level code environment — Python 3.10.6 documentation
Both of these mechanisms are related to Python modules; how users interact with them and how they interact with each other. They are explained in detail below. If you’re new to Python modules, see the tutorial section Modules for an introduction. __name_
docs.python.org
When a Python module or package is imported, __name__ is set to the module’s name. Usually, this is the name of the Python file itself without the .py extension:
파이썬 모듈이나 패키지가 import 될 때 __name__이 그 모듈의 이름을 설정한다.
보통 .py 확장자명을 제외한 파이썬 파일 이름이다.
However, if the module is executed in the top-level code environment, its __name__ is set to the string '__main__'.
만약 모듈이 최상위 코드 환경에서 호출될 경우 __name__은 문자열 '__main__'로 설정된다.
Flask에서 if __name__ == '__main__': 에서 사용
1
2
3
4
5
6
7
8
9
10
11
12
|
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
if __name__ == '__main__':
app.run()
|
cs |
Flask는 __name__을 받으면 현재 실행 중인 파일에 애플리케이션 코드가 있는지 확인한다.
print(__name__)
>>> __main__