LoginSignup
1
3

More than 3 years have passed since last update.

ANACONDAやめました MacにPython開発環境構築 pyenvとpyenv-virtualenvのインストール

Last updated at Posted at 2021-01-11

ANACONDAやめました MacにPython開発環境構築 pyenvとpyenv-virtualenvのインストール

目的

Python開発環境の作業メモとして記載。
ANACONDAのライセンスが変わり、当面は問題無いのですが、ANACONDAを使わないPythonのローカル開発環境へ移行してみました。
Pythonの開発環境としては、pyenvとpyenv-virtualenvを利用、pythonのバージョンが切替可能とする方法を選択しました。
pyenvとpyenv-virtualenvのインストールは、楽なのでhomebrewを使います。

Mac環境

OS : Mac OS Big Sur(11.1)
Xcode : App Storeではインストールしていない

インストール

homebrewのアップデート

taguchi@MasamacAir ~ % brew update
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
acl2                       grpcui                     ocaml-zarith
act                        gtkmm4                     oci-cli
aerc                       guile@2                    odin
aida-header                gulp-cli                   oha
airshare                   h2spec                     omake

省略

iphoney                                  yandexradio
java                                     yourkit-java-profiler
jeromelebel-mongohub                     zoom-in
jing                                     zoomus-outlook-plugin
kekadefaultapp

pyenvのインストール

taguchi@MasamacAir ~ % brew install pyenv
Error: 
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.

省略

==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

pyenv-virtualenvのインストール

taguchi@MasamacAir ~ % brew install pyenv-virtualenv

Error: Your CLT does not support macOS 11.
It is either outdated or was modified.
Please update your CLT or delete it if no updates are available.
Update them from Software Update in System Preferences or run:
  softwareupdate --all --install --force

If that doesn't show you an update run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/more/.

Error: An exception occurred within a child process:
  SystemExit: exit

pyenv-virtualenvをインストールしようとしたが、エラーになった、対応

taguchi@MasamacAir ~ % softwareupdate --all --install --force

  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

taguchi@MasamacAir ~ % sudo rm -rf /Library/Developer/CommandLineTools
Password:
taguchi@MasamacAir ~ % sudo xcode-select --install

省略

リトライ pyenv-virtualenvをインストール

taguchi@MasamacAir ~ % brew install pyenv-virtualenv
Error: 
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:

省略

==> ./install.sh
==> Caveats
To enable auto-activation add to your profile:
  if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
==> Summary
🍺  /usr/local/Cellar/pyenv-virtualenv/1.1.5: 22 files, 65.6KB, built in 5 seconds

pyenvの設定

taguchi@MasamacAir ~ % echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.zprofile
taguchi@MasamacAir ~ % echo 'export PATH="${PYENV_ROOT}/bin:$PATH"' >> ~/.zprofile
taguchi@MasamacAir ~ % echo 'eval "$(pyenv init -)"' >> ~/.zprofile 
taguchi@MasamacAir ~ % echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zprofile 

pyenv環境設定

使えるバージョンを確認

taguchi@MasamacAir ~ % pyenv install --list      
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0

省略

Python 3.9.1 と 2.7.18 をインストール

Python 3.9.1

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

Python 2.7.18

taguchi@MasamacAir ~ % pyenv install 2.7.18 
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-2.7.18.tar.xz...
-> https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
Installing Python-2.7.18...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-2.7.18 to /Users/taguchi/.pyenv/versions/2.7.18

Pythonのバージョン切替方法

開発用のディレクトリ毎に利用するPythonのバージョンを変える(localコマンド)

使い方

cd <開発用ディレクトリ>
pyenv local <この開発用ディレクトリで利用するPythonのバージョン>

実行例
デフォルトはPython 2.7.16 だが 開発ディレクトリpy2で利用するPythonのバージョンを 2.7.18 に変更する

taguchi@MasamacAir ~ % cd ~/test/py2
taguchi@MasamacAir py2 % python --version
Python 2.7.16
taguchi@MasamacAir py2 % pyenv local 2.7.18
taguchi@MasamacAir py2 % python --version
Python 2.7.18

python --version の表示が変わらないとき
1)よくある 他のディレクトリに移動してから再度戻る、ログアウトしてログインし直してから再度確認してみてください
2).zprofileや/etc/paths でパス/usr/local/binが/usr/binより優先されているかを確認してください

システム全体で利用するPythonのバージョンを変える(globalコマンド)

pyenv global <システム全体で利用するPythonのバージョン>

実行例
デフォルトはPython 2.7.16 だが システム全体で利用するPythonのバージョンを 3.9.1 に変更する

taguchi@MasamacAir ~ % python --version
Python 2.7.16
taguchi@MasamacAir ~ % pyenv global 3.9.1
taguchi@MasamacAir ~ % python --version
Python 3.9.1

pyenv-virtualenv環境設定

pyenv-virtualenvを使うと、開発環境が分けられます。
ライブラリ等が異なるなどの際に便利です。
私は、色々試したい場合にはpyenv-virtualenv、開発環境の構成が複雑な場合や開発環境を共有する場合はDocker(ローカルPCやクラウド上)やVirtualBox(ローカルPC)と使い分けています。

使い方

pyenv virtualenv <利用するPythonのバージョン> <Python開発環境の名前、自分でわかるように付ける>

実行例
Pythonのバージョン 2.7.18 の開発環境 GANtest1 を作る、開発ディレクトリは ~/test/GANtest1

taguchi@MasamacAir ~ % mkdir ~/test/GANtest1
taguchi@MasamacAir ~ % cd ~/test/GANtest1
taguchi@MasamacAir GANtest1 % pyenv virtualenv 2.7.18 GANtest1
taguchi@MasamacAir GANtest1 % pyenv local GANtest1
taguchi@MasamacAir GANtest1 % python --version
Python 2.7.18

作成した開発環境が不要になったら、以下で消すことができます
使い方

pyenv uninstall <Python開発環境の名前>

実行例

taguchi@MasamacAir ~ % pyenv uninstall GANtest1
taguchi@MasamacAir ~ % rm -r ~/test/GANtest1
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3