파이썬 import 실패
에러 예시
$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
위와 같이 파이썬 모듈 import에 실패할 수 있다.
일반적인 경우 pip install 명령을 통해 해당 모듈을 설치해주면 문제가 해결되지만,
일부 환경의 경우 설치를 시도하더라도 다음과 같이 이미 설치되어있다는 문구가 노출될 것이다.
$ pip install requests
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.25.1)
문제의 원인
pip 버전과 파이썬 버전을 확인해보면 pip에서 지원하는 버전과 사용하는 파이썬의 버전이 상이할 것이다.
$ pip -V
python3/dist-packages/pip (python 3.10)
$ python -V
Python 3.11.3
해결방법
sudo apt install python3-pip -y 명령이나, python{파이썬 버전} -m pip install --upgrade pip 명령을 통해 pip를 설치해보자.
sudo apt install python3-pip -y
python3.11 -m pip install --upgrade pip
이렇게 pip 설치를 시도하더라도 문제해결이 되지 않는다면, 이 글을 참고해보자.
임시방편
"python{파이썬 버전} -m pip install <module>" 명령으로 버전을 특정하여 모듈을 설치할 수 있다.
명령어가 길어지는 것을 신경쓰지 않는다면 이 방법을 사용하면 된다.
$ python3.10 -m pip install requests
$ python
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>