-
python 실행 vs module 실행/ jupyter notebook 실행 vs shell 실행 self checkCS 2022. 6. 17. 11:34
- python 파일 실행 vs module로 import 실행
파이썬에 argument 입력하고 싶을때 등장하는 것이
if __name__ == “__main__” : print('바로 py파일 실행할때') else : print('import 해서 모듈처럼 사용할때')
그런데 나는 인터프리터에서 사용 vs terminal에서 실행 이 둘을 구별하고 싶은데 그이유는 ipynb 파일에서 예시로 몇개 실행해보고 py로 바꾸기 때문이다.
즉 내가 원하는 두 케이스다 __name__ == “__main__” 로 인식된다.
- 인터프리터에서 사용 vs terminal에서 실행
def isnotebook(): try: shell = get_ipython().__class__.__name__ if shell == 'ZMQInteractiveShell': return True # Jupyter notebook or qtconsole elif shell == 'TerminalInteractiveShell': return False # Terminal running IPython else: return False # Other type (?) except NameError: return False # Probably standard Python interpreter
최종적으로 활용예시는 다음과 같다
if isnotebook(): ## jupyter notebook 내에서만 실행 else : ## shell에서 실행 if len(sys.argv) != 2: print("Insufficient arguments") else : {$variable} = sys.argv[1]
Reference:
'CS' 카테고리의 다른 글
yaml 파일이란? (0) 2022.07.18 [linux] root directory ('/') vs home directory ('~') (0) 2022.07.03 jenkins 에서 작업한 파일을 github 연동하기 (0) 2022.06.28 Spark 설치 과정 - hadoop, hive, mariadb 의 관계 (0) 2022.06.12 Airflow vs. Jenkins vs. cron (0) 2022.06.06