programming language/Python

[Python] try, except를 활용해서 import와ModuleNotFoundError시에 install하기

눈곱만큼도모름 2023. 8. 25. 14:32
728x90
반응형

try, except를 활용해서 import와ModuleNotFoundError시에  install하기

try:
    from matplotlib import pyplot as plt
except ModuleNotFoundError:
    import pip
    pip.main(['install', 'matplotlib'])
    try:
        from matplotlib import pyplot as plt
    except ModuleNotFoundError:
        time.sleep(2)
        from matplotlib import pyplot as plt

try:
    import numpy as np
except ModuleNotFoundError:
    import pip
    pip.main(['install', 'numpy'])
    try:
        import numpy as np
    except ModuleNotFoundError:
        time.sleep(2)
        import numpy as np
728x90
반응형

'programming language > Python' 카테고리의 다른 글

Jupyter notebook 단축키  (2) 2023.11.19
[Python] any()  (0) 2023.08.22
[Python] enumerate 함수  (0) 2023.07.24
[Python] conditional expression, list comprehension  (0) 2023.07.18
[Python] 연산관련 function과 method  (0) 2023.07.18