MacOS 환경에서 Python3 requests 설치하여 사용하는 법

MacOS Sonoma 이상에서는 터미널에서의 권한이 상당히 제약이 많아서, 안되는 것들이 많습니다. Python3 에서 requests 를 사용하는 스크립트를 실행하기 위해 아래와 같이 입력하면 경고문이 잔뜩 뜹니다.

user % pip3 install requests
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:
    
    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz
    
    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with
    
    brew install pipx
    
    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.
    
    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.
    
    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

이 오류는 macOS의 Python이 Homebrew 또는 시스템에 의해 외부에서 관리되고 있어서, pip이 시스템 전체에 직접 패키지를 설치하지 못하도록 막고 있기 때문입니다. 이는 Python 환경을 보호하기 위한 PEP 668의 일환이라고 하는데, 아무튼 왠만하면 쓰지 말라는 말인 것 같습니다.

그래서, 모든 걸 무시하고 과감하게 하는 방법들이 있지만, 나중에 무슨 일이 있을지 모르니까 좀 안전하게 권장하는 방법대로 해보겠습니다.

제일 안전하고 권장하는 방법은 가상환경(venv)에서 하는 법이라고 합니다. 이렇게 하면, 좀 귀찮기는 한데 잘 작동합니다.

python3 -m venv venv
source venv/bin/activate
pip install requests

이렇게 python3에 venv 환경을 잡아준 후에, 해당 가상환경을 activate 하고 나서 requests를 인스톨하면, 아무일 없었다는 듯이 자연스럽게 설치가 됩니다.


....
Collecting requests
  Using cached requests-2.32.4-py3-none-any.whl.metadata (4.9 kB)
Collecting charset_normalizer<4,>=2 (from requests)
  Using cached charset_normalizer-3.4.2-cp313-cp313-macosx_10_13_universal2.whl.metadata (35 kB)
Collecting idna<4,>=2.5 (from requests)
  Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
  Using cached urllib3-2.5.0-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests)
  Using cached certifi-2025.7.14-py3-none-any.whl.metadata (2.4 kB)
Using cached requests-2.32.4-py3-none-any.whl (64 kB)
Using cached charset_normalizer-3.4.2-cp313-cp313-macosx_10_13_universal2.whl (199 kB)
Using cached idna-3.10-py3-none-any.whl (70 kB)
Using cached urllib3-2.5.0-py3-none-any.whl (129 kB)
Using cached certifi-2025.7.14-py3-none-any.whl (162 kB)
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2025.7.14 charset_normalizer-3.4.2 idna-3.10 requests-2.32.4 urllib3-2.5.0

위와 같이 인스톨 한후에는 prompt에 가상환경을 나타내는 (venv) 가 표시됩니다.
venv 환경에 requests를 인스톨했으니 앞으로 venv를 activate 하면 해당 환경에 설치된 라이브러리들이 잘 작동합니다.

다음에 터미널을 다시 연 후에는 아래와 venv환경을 설정하고, 사용하면 됩니다.

user@iMac iMacEXT % source venv/bin/activate 
(venv) user@iMac iMacEXT % python3 python_script.py

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다