Python
[Python] VS Code에서 ModuleNotFound 해결하기
Jisu_Park
2023. 2. 20. 12:28
python에서 streamlit을 활용한 파일을 열려는 도중 에러 발생
> ModuleNotFoundError: no module named 'matplotlib'
가상환경 접속 되어있었으며, 이미 설치된 상태였고 다른 라이브러리도 불러와지지 않았음.
시도 1. 환경변수에서 Path 설정
- 시작 메뉴 > 설정 > 시스템 > 정보 탭 > 고급 시스템 설정 > 환경변수 >
시스템 설정의 Path 편집에 Python 설치 경로와 실행 파일의 lib 경로를 추가했고 혹시 몰라 Python 내의 lib 경로도 추가함
시도 2. Python interpreter 설정
1)과 2)는 같은 방법
1) VS Code에서 Ctrl+Shift+P 입력 후 뜨는 창에 Python Select Interpreter 검색 및 선택 후 Path 지정
2) 상단 File 탭 > Preferences > Python: Default Interpreter Path 검색 후 뜨는 란에 python 혹은 python3 기입
- 검색시 Python Interpreter가 뜨지 않는다면 Python Extension이 설치되지 않은 것이므로
VS Code에서 왼쪽 블럭모양 메뉴(Extensions, Ctrl+Shift+X) > Python Extension 검색 > Python Extension Pack 설치
위 시도한 것들 둘 다 실패했고 Git bash에서 가상환경을 종료하고 다시 실행했더니 정상적으로 실행됐다
$ python
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl # matplotlib 설치 확인
>>> exit() # python 창 나가기
$ deactivate # 가상환경 종료
$ source venv/Script/activate # source 다음은 가상환경 설치경로
그외에 구글링 결과중에 import sys해서 print(sys.path)하는 방법도 있었으나 시도하진 않음