yoshiislandblog.net
元営業の駆け出しアラサーSEが、休日にMACと戯れた際の殴り書きメモ。日々勉強。日々進歩。

この記事は3年以上前に書かれた記事で内容が古い可能性があります

Macにpyenvとvirtualenvを入れる

2018-11-13

この手の記事たくさん書いた気がするが、真っ新な新しいMacに入れたので、懲りずに書く
サクッとamazon linuxにpyenvとpython3を導入
pyenv virtualenvの設定
Ubuntu14にpyenvとPython3環境導入

pyenv、virtualenvとはなんぞや、というのはこちらをご参考に
pyenvとpyenv-virtualenvとvirtualenvとの違い

pyenvをインストールする

% brew install pyenv
Updating Homebrew...
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).

...
==> Installing dependencies for pyenv: autoconf and pkg-config
==> Installing pyenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.high_sierra.bottle.4.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.high_sierra.bottle.4.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
🍺  /usr/local/Cellar/autoconf/2.69: 71 files, 3.0MB
==> Installing pyenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.2.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.2: 11 files, 627.2KB
==> Installing pyenv
==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.2.8.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pyenv-1.2.8.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pyenv/1.2.8: 612 files, 2.4MB
==> Caveats
==> autoconf
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf

インストールされているか確認

% pyenv
pyenv 1.2.8
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

続いてvirtualenvをインストール

% brew install pyenv-virtualenv
==> Downloading https://github.com/pyenv/pyenv-virtualenv/archive/v1.1.3.tar.gz
==> Downloading from https://codeload.github.com/pyenv/pyenv-virtualenv/tar.gz/v1.1.3
######################################################################## 100.0%
==> ./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.3: 20 files, 62.2KB, built in 7 seconds

パスを通す。自分の環境はzshなので、zshrcに記載

% tail -6 ~/.zshrc
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
fi

pyenvにpython3.5.0をインストール

% pyenv versions
* system (set by /Users/yoshi/.pyenv/version)
% pyenv install 3.5.0
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.0.tar.xz...
-> https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
Installing Python-3.5.0...
python-build: use readline from homebrew
Installed Python-3.5.0 to /Users/yoshi/.pyenv/versions/3.5.0

インストールされている

% pyenv versions
  system
* 3.5.0 (set by /Users/yoshi/django_work/.python-version)

python3.5.0で、django_workという名前で、virtualenvを作成する

% pyenv virtualenv 3.5.0 django_work

virtualenvができている

% pyenv versions
  system
* 3.5.0 (set by /Users/yoshi/django_work/.python-version)
  3.5.0/envs/django_work
  django_work

local環境に先ほど作成したdjango_workを適用する

% pyenv local django_work

適用されている

% pyenv versions
  system
  3.5.0
  3.5.0/envs/django_work
* django_work (set by /Users/yoshi/django_work/.python-version)
% python --version
Python 3.5.0

以上