본문 바로가기
★━공부하기〃/1. Python

MAC OS에 Python을 pyenv pyenv-virtualenv 설치 하기

by Raynee 2022. 3. 25.
반응형

Home Brew 를 통해 pyenv 와 pyenv-virtualenv를 설치한다. 

설치는 간단하다. 명령어 한줄 이면 설치가 완료 된다. 

brew install pyenv pyenv-virtualenv

 

사용법에 대해 알아 보자 

현재 사용 하고있는 python의 버전 확인, 현재는 local python을 사용 하고 있다.

# pyenv version                                       
system (set by /Users/mskim/.pyenv/version)

 

현재 깔려 있는 Python의 버전 확인. local python과 새로 설치한 3.10.3 버전이 있는 걸 확인 할 수 있다. 

현재 적용된 버전은 Local python 인 system으로 되어 있다. 

# pyenv versions                                      
* system (set by /Users/mskim/.pyenv/version)
  3.10.3

 

설치 할 Pythone list 확인

# pyenv install --list                               
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  
  ....
  
  stackless-3.3.5
  stackless-3.3.7
  stackless-3.4-dev
  stackless-3.4.2
  stackless-3.4.7
  stackless-3.5.4
  stackless-3.7.5

 

python 다른 버전 설치 하기

pythone 3.10.3 버전을 설치 해보겠습니다. 

# pyenv install 3.10.3                                 
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.3.tar.xz...
-> https://www.python.org/ftp/python/3.10.3/Python-3.10.3.tar.xz
Installing Python-3.10.3...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.3 to /Users/mskim/.pyenv/versions/3.10.3

# pyenv versions                                       
* system (set by /Users/mskim/.pyenv/version)
  3.10.3

local shell에서 pythone을 변경하여 사용 할 수 다. 하지만 pyenv-virtualenv 가상에서 사용하는게 좋다. 

먼저 local에서 pythone 버전을 변경 해보자.  system python인 3.9.7에서 새로 받은 3.10.3 으로 변경 되었다.

# python3                                               
Python 3.9.7 (default, Oct 13 2021, 06:45:31)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>

# pyenv shell 3.10.3                            

# pyenv version                                   
3.10.3 (set by PYENV_VERSION environment variable)

# python3                                              
Python 3.10.3 (main, Mar 25 2022, 12:15:39) [Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

 

 

pyenv-virtualenv를 이용하여 사용 해보자 

가상으로 해서 좋은점은 관리 면이다. local shell로 사용 하면 3.10.3 여러개를 여러 환경에 맞게 구성 할 수도 있다. 

3.10.1 버전을 test1 

# pyenv virtualenv 3.10.3 test1

# pyenv versions                             
* system (set by PYENV_VERSION environment variable)
  3.10.3
  3.10.3/envs/test1
  test1
  
# pyenv activate test1                            

# pyenv version                               
test1 (set by PYENV_VERSION environment variable)

Python 3.10.3인 test1에  jupyter를 설치 해보자

설친한 jupyter를 실행 해보면 웹브라우저가 잘실행 된다. 

# pip3 install jupyter                         ✔   test1  12:37:19
Collecting jupyter
  Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting ipywidgets
  Downloading ipywidgets-7.7.0-py2.py3-none-any.whl (123 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 123.4/123.4 KB 7.6 MB/s eta 0:00:00
Collecting jupyter-console
  Downloading jupyter_console-6.4.3-py3-none-any.whl (22 kB)\
 
 ....
 
 # jupyter notebook                     ✔   22s   test1  12:40:06
[I 12:40:17.291 NotebookApp] Writing notebook server cookie secret to /Users/mskim/Library/Jupyter/runtime/notebook_cookie_secret
[I 12:40:17.887 NotebookApp] Serving notebooks from local directory: /Users/mskim
[I 12:40:17.887 NotebookApp] Jupyter Notebook 6.4.10 is running at:
[I 12:40:17.887 NotebookApp] http://localhost:8888/?token=b33fbd36a108c7bf102763995aa2b016b3a93b49bccdc992
[I 12:40:17.888 NotebookApp]  or http://127.0.0.1:8888/?token=b33fbd36a108c7bf102763995aa2b016b3a93b49bccdc992
[I 12:40:17.888 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:40:17.899 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///Users/mskim/Library/Jupyter/runtime/nbserver-81549-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=b33fbd36a108c7bf102763995aa2b016b3a93b49bccdc992
     or http://127.0.0.1:8888/?token=b33fbd36a108c7bf102763995aa2b016b3a93b49bccdc992

Local Shell로 나와서 jupyter notebook를 실행하면 실행이 안된다. 친절하게 test1의 version에 있다고 알려준다. 

# pyenv version                            
test1 (set by PYENV_VERSION environment variable)

# pyenv deactivate test1                   

# pyenv version                                         
system (set by /Users/mskim/.pyenv/version)

# jupyter notebook                                
pyenv: jupyter: command not found

The `jupyter' command exists in these Python versions:
  3.10.3/envs/test1
  test1

Note: See 'pyenv help global' for tips on allowing both
      python2 and python3 to be found.

Python 3.10.3 의 다른 버전인 test2의 가상환경을 만들 수도 있다. 그러면 3.10.3의 두가지 버전을 다른 환경에 맞게 개발 할 수 있게 된다. 

 

가상환경인 test1을 삭제 해보자

# pyenv versions                                     
* system (set by /Users/mskim/.pyenv/version)
  3.10.3
  3.10.3/envs/test1
  test1


 # pyenv uninstall test1                                 
pyenv-virtualenv: remove /Users/mskim/.pyenv/versions/3.10.3/envs/test1? y


# pyenv versions                               
* system (set by /Users/mskim/.pyenv/version)
  3.10.3

 

 

 

 

장애 대응 

pyenv 를 실행 할때 shell 장애가 났을 경우.

사용 하고 있는데 Shell의 rc 파일 하단에 변수값을 설정 해줘야 한다. 

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

fish shell 일 경우.

# vi ~/.config/fish/config.fish

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

 

zsh shell 일 경우 

# vi $HOME/.oh-my-zsh     
제일 하단에 아래 내용 붙여 넣기 

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

 

아래와 같이 PYENV_VIRTUALENV_DISABLE_PROMPT=1 넣으라고 나온다. 

❯ pyenv activate ms
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

zshrc 파일에 변수 값을 넣어 주면된다. 

 

 

 

반응형

댓글