생산성&도구

[Mac] Homebrew로 Jupyter 설치 & pip3 install 오류 해결방법

엘레나림 2024. 7. 2. 12:31
728x90

Jupyter 사용하기 위해 Anaconda를 설치하라는 경우가 있는데, 예전에 Anaconda 를 사용했을 때 앱이 꽤 무거웠던 기억이 있고 이미 내 PC에는 python이 설치되어있기 때문에!

이번에는 brew install 로 jupyter 만 따로 설치해보았다.

 

(Anaconda로 설치하게 되면 가상환경이나 패키지 관리를 쉽게 할 수 있다는 장점이 있음. Anaconda 설치와 함께 파이썬 에디터로써 Jupyter notebook도 자동으로 설치됨)

 

 

 

homebrew 로 주피터 노트북 설치

 

나는 보통 웬만한 프로그램이나 툴은 brew install 로 설치를 해서 쓰고 있다. 

# 주피터 노트북 설치
brew install jupyter

# 주피터 노트북 버전 확인
jupyter --version

# 주피터 노트북 실행
jupyter notebook

 

주피터 노트북을 실행하면 기본 웹 브라우저로 jupyter notebook 인터페이스가 열린다.

주소는 http://localhost:8890/tree 

이제 터미널 창은 jupyter notebook의 서버가 되는 것이다.

 

 

 

주피터 노트북에서 명령어 pip 실행

 

python3 ipykernel 파일을 하나 생성한다.

python 코드 뿐 아니라 터미널 명령어도 실행할 수 있는데, 맨 앞에 ! 를 쓰고 명령어를 입력하면 된다.

 

명령어 설명 jupyter notebook 사용 예시
pwd 현재 어떤 경로의 디렉토리에 있는지 출력 !pwd
ls -al 디렉토리 파일 목록 전체를 list 형태로 조회 !ls -al
mkdir 디렉토리 생성 !mkdir study
rmdir 디렉토리 삭제 !rmdir study

 

 

여기서 패키지 설치를 위해,

! pip3 install selenium 를 하게 되면 이런 error 가 뜨게 된다...!

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.

[notice] A new release of pip is available: 24.0 -> 24.1.1
[notice] To update, run: python3.12 -m pip install --upgrade pip

 

표시되는 오류 메시지는 Python 환경이 외부에서 관리됨을 나타내는 메커니즘인 PEP 668과 관련이 있습니다. 이는 일반적으로 Homebrew에서 관리하는 Python 환경이 pip를 사용하여 시스템 전체 패키지 설치를 방지하도록 설정되었음을 의미합니다. 대신 가상 환경이나 'pipx'와 같은 도구를 사용하는 등 시스템 Python 환경과의 충돌을 피하는 패키지 관리 방식을 사용하도록 권장합니다.

 

homebrew로 설치한 python 환경에서 pip으로 전역 패키지 설치를 못하게 하는 것이라고 한다.

그래서 pipx 를 이용해서도 해봤고 강제로도 실행을 해봤다! 

!pipx install selenium --include-deps --force

done 이라고는 출력 됐지만, 환경 경로 뭐시기 또 경고가 떴고 제대로 설치되지 않았다..... 

 

 

 

그러다가 주피터 커뮤니티 포럼에서 이 글을 보고 해결했다!!!!

 

! 대신 % 를 쓰면 설치된 환경에 맞게 설치 가 된다는 것이다 (IPython v7.3 이상에서 추가된 개선사항이라고 함)

%pip install selenium

 

https://discourse.jupyter.org/t/unable-to-run-selenium-on-jupyter-notebook-masos-catalina/9279

 

Unable to run selenium on Jupyter Notebook (MasOS, Catalina)

Hello I am unable to run selenium on Jupyter notebook, where Anaconda is my python distribution platform. However, I am able to run selenium using the Python IDLE Shell 3.9.1 and use the Chromedriver in this shell. This is what shows in jupyter notebook: !

discourse.jupyter.org

 

python이 어디에 설치되었고 jupyter는 어떤 환경에서 실행되고 있는지를 모를 때.

나와 비슷한 경우라면, pip 앞에 % 를 쓰면 만능으로 해결될 것으로 보인다!

 

 

+

 

(1) 추가로 내 jupyter에서의 python 인터프리터의 실행파일 경로를 알아내기 위해서 이 명령어를 썼다.

import sys
sys.executable

 

output ->

'/opt/homebrew/Cellar/jupyterlab/4.2.1/libexec/bin/python'

 

 

(2) 실행중인 python 인터프리터에서 pip을 upgrade 해야하는 경우,

!/opt/homebrew/Cellar/jupyterlab/4.2.1/libexec/bin/python -m pip install --upgrade pip