Edited at

MacのPython環境をpyenvでセットアップ + awscliのインストール

More than 3 years have passed since last update.

MacのPython環境をpyenvでセットアップして、awscliをインストールします。


pyenvとは

複数のPythonバージョンの環境を切り替えて使うためのソフトウェア。


pyenvのインストール


Homebrewでインストール

$ brew install pyenv

==> Downloading https://github.com/yyuu/pyenv/archive/v20141106.tar.gz
######################################################################## 100.0%
==> Caveats
To enable shims and autocompletion add to your profile:
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew's directories rather than ~/.pyenv add to your profile:
export PYENV_ROOT=/usr/local/opt/pyenv
==> Summary
🍺 /usr/local/Cellar/pyenv/20141106: 307 files, 2.6M, built in 4 seconds

$ pyenv --version
pyenv 20141106


shimsへのパス追加 & 自動補完の有効化

ターミナル起動時に、shimsへのパス追加 & 自動補完を有効化するように、~/.bash_profile に以下を追加。

(shims: Shims are lightweight executables that simply pass your command along to pyenv.)

$ echo 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi' >> ~/.bash_profile 


環境変数

$PYENV_ROOTを設定するとインストール先を指定できる。

デフォルトでは、~/.pyenv/versions/以下にバージョン毎のディレクトリにインストールされる。


pyenv環境にPythonをインストール


今使っているPythonバージョンを表示

システムデフォルトのPython

$ pyenv version

system (set by /Users/takech9203/.pyenv/version)

$ cat .pyenv/version
system

$ python --version
Python 2.7.6


インストール済みのバージョンを確認

$ pyenv versions

* system (set by /Users/takech9203/.pyenv/version)
3.3.2


pyenvでインストール可能なPythonバージョンの表示

$ pyenv install -l

Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
2.6.7
2.6.8
2.6.9
2.7-dev
2.7
2.7.1
2.7.2
2.7.3
2.7.4
2.7.5
2.7.6
2.7.7
2.7.8
3.0.1
3.1-dev
3.1.3
3.1.4
3.1.5
3.2-dev
3.2
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6
3.3.0
3.3-dev
3.3.1
3.3.2
3.3.3
3.3.4
3.3.5
3.3.6
3.4.0
3.4-dev
3.4.1
3.4.2
anaconda-1.4.0
anaconda-1.5.0
anaconda-1.5.1
anaconda-1.6.0
...


Pythonのインストール

$ pyenv install 3.4.2

Downloading readline-6.3.tar.gz...
-> http://ftpmirror.gnu.org/readline/readline-6.3.tar.gz
Installing readline-6.3...
Installed readline-6.3 to /Users/takech9203/.pyenv/versions/3.4.2

Downloading Python-3.4.2.tgz...
-> https://yyuu.github.io/pythons/44a3c1ef1c7ca3e4fd25242af80ed72da941203cb4ed1a8c1b724d9078965dd8
Installing Python-3.4.2...
Installed Python-3.4.2 to /Users/takech9203/.pyenv/versions/3.4.2


ERROR: The Python ssl extension was not compiled. の場合

xcode command line developer toolsがインストールされていないと、pyenvでのPythonインストール時に以下のエラーがでる。

$ pyenv install 3.4.3 

...

Ignoring ensurepip failure: pip 6.0.8 requires SSL/TLS
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/yyuu/pyenv/wiki/Common-build-problems

BUILD FAILED (OS X 10.10.4 using python-build 20150519)

xcode command line developer tools をインストールして解決。

$ sudo xcode-select --install


インストールの確認

$ pyenv versions

* system (set by /Users/takech9203/.pyenv/version)
3.3.2
3.4.2


バージョンの変更

# システムデフォルトを使用

$ pyenv global
system

# Python 3.3.2に変更
$ pyenv global 3.3.2
$ pyenv versions
system
* 3.3.2 (set by /Users/takech9203/.pyenv/version)
3.4.2
$ pyenv version
3.3.2 (set by /Users/takech9203/.pyenv/version)
$ cat .pyenv/version
3.3.2

# 実行
$ python --version
Python 3.3.2


awscliのインストール

ここでawscliをインストールしてみる


system 環境のawscliの削除

これまでsystemのPython環境にインストールして使っていたawscliはあらかじめ削除

$ pyenv global system

$ sudo pip uninstall awscli
$ sudo pip uninstall awsebcli


pyenv以下にawscliをインストール

# Python 3.4.2の環境

$ pyenv global 3.4.2
$ pyenv version
3.4.2 (set by /Users/takech9203/.pyenv/version)

# awsebcliをpipでインストール. ~/.pyenv/versions/3.4.2/ 以下にインストールされる.
$ pip install awscli
Downloading/unpacking awscli
Downloading awscli-1.7.34.tar.gz (357kB): 357kB downloaded
Running setup.py (path:/private/var/folders/d4/v4zwr_5j5zl1tm3rgvp4lz540000gn/T/pip_build_takech9203/awscli/setup.py) egg_info for package awscli

Downloading/unpacking botocore==1.0.0b3 (from awscli)
Downloading botocore-1.0.0b3.tar.gz (1.6MB): 1.6MB downloaded
Running setup.py (path:/private/var/folders/d4/v4zwr_5j5zl1tm3rgvp4lz540000gn/T/pip_build_takech9203/botocore/setup.py) egg_info for package botocore

Downloading/unpacking bcdoc>=0.16.0,<0.17.0 (from awscli)
Downloading bcdoc-0.16.0-py2.py3-none-any.whl
Downloading/unpacking colorama>=0.2.5,<=0.3.3 (from awscli)
Downloading colorama-0.3.3.tar.gz
Running setup.py (path:/private/var/folders/d4/v4zwr_5j5zl1tm3rgvp4lz540000gn/T/pip_build_takech9203/colorama/setup.py) egg_info for package colorama

Downloading/unpacking docutils>=0.10 (from awscli)
Downloading docutils-0.12-py3-none-any.whl (508kB): 508kB downloaded
Downloading/unpacking rsa>=3.1.2,<=3.1.4 (from awscli)
Downloading rsa-3.1.4.tar.gz
Running setup.py (path:/private/var/folders/d4/v4zwr_5j5zl1tm3rgvp4lz540000gn/T/pip_build_takech9203/rsa/setup.py) egg_info for package rsa

warning: no files found matching 'README'
Downloading/unpacking jmespath==0.7.1 (from botocore==1.0.0b3->awscli)
Downloading jmespath-0.7.1-py2.py3-none-any.whl
Downloading/unpacking python-dateutil>=2.1,<3.0.0 (from botocore==1.0.0b3->awscli)
Downloading python_dateutil-2.4.2-py2.py3-none-any.whl (188kB): 188kB downloaded
Downloading/unpacking six>=1.8.0,<2.0.0 (from bcdoc>=0.16.0,<0.17.0->awscli)
Downloading six-1.9.0-py2.py3-none-any.whl
Downloading/unpacking pyasn1>=0.1.3 (from rsa>=3.1.2,<=3.1.4->awscli)
Downloading pyasn1-0.1.7.tar.gz (68kB): 68kB downloaded
Running setup.py (path:/private/var/folders/d4/v4zwr_5j5zl1tm3rgvp4lz540000gn/T/pip_build_takech9203/pyasn1/setup.py) egg_info for package pyasn1

Installing collected packages: awscli, botocore, bcdoc, colorama, docutils, rsa, jmespath, python-dateutil, six, pyasn1
Running setup.py install for awscli
changing mode of build/scripts-3.4/aws from 644 to 755
changing mode of build/scripts-3.4/aws.cmd from 644 to 755
changing mode of build/scripts-3.4/aws_completer from 644 to 755
changing mode of build/scripts-3.4/aws_zsh_completer.sh from 644 to 755

changing mode of /Users/takech9203/.pyenv/versions/3.4.2/bin/aws to 755
changing mode of /Users/takech9203/.pyenv/versions/3.4.2/bin/aws.cmd to 755
changing mode of /Users/takech9203/.pyenv/versions/3.4.2/bin/aws_completer to 755
changing mode of /Users/takech9203/.pyenv/versions/3.4.2/bin/aws_zsh_completer.sh to 755
Running setup.py install for botocore

Running setup.py install for colorama

Running setup.py install for rsa

warning: no files found matching 'README'
Installing pyrsa-decrypt script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-verify script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-keygen script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-encrypt-bigfile script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-decrypt-bigfile script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-sign script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-priv2pub script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Installing pyrsa-encrypt script to /Users/takech9203/.pyenv/versions/3.4.2/bin
Running setup.py install for pyasn1

Successfully installed awscli botocore bcdoc colorama docutils rsa jmespath python-dateutil six pyasn1
Cleaning up...

# awsebcliのインストール
$ pip install awsebcli


インストール後はshimsを更新してパスを認識させる

# そのままだとパスが見つからない

$ aws
-bash: /usr/local/bin/aws: No such file or directory

# shimsを更新
$ pyenv rehash

# 実行
$ aws --version
aws-cli/1.7.34 Python/3.4.2 Darwin/14.3.0


  • pyenvのrehashを自動化するpyenv-pip-rehashというプラグインを使ってもよい

brew install pyenv-pip-rehash


aws cli completer の設定変更

~/.bash_profileに設定していたauto completerが絶対パスに見つからないのでエラーになる

$ aws ec2 describe-<ここで[TAB]を押下>-bash: /usr/local/bin/aws_completer: No such file or directory

絶対パスで設定していたのをpyenv環境の相対パスに変更

#complete -C '/usr/local/bin/aws_completer' aws

complete -C 'aws_completer' aws

修正後、ターミナルを立ち上げ直すと[TAB]キーで正常に候補が表示される

$ aws ec2 describe-<ここで[TAB]を押下>

describe-account-attributes describe-key-pairs describe-spot-fleet-request-history
describe-addresses describe-moving-addresses describe-spot-fleet-requests
describe-availability-zones describe-network-acls describe-spot-instance-requests
describe-bundle-tasks describe-network-interface-attribute describe-spot-price-history
describe-classic-link-instances describe-network-interfaces describe-subnets
describe-conversion-tasks describe-placement-groups describe-tags
describe-customer-gateways describe-prefix-lists describe-volume-attribute
describe-dhcp-options describe-regions describe-volume-status
describe-export-tasks describe-reserved-instances describe-volumes
describe-flow-logs describe-reserved-instances-listings describe-vpc-attribute
describe-image-attribute describe-reserved-instances-modifications describe-vpc-classic-link
describe-images describe-reserved-instances-offerings describe-vpc-endpoint-services
describe-import-image-tasks describe-route-tables describe-vpc-endpoints
describe-import-snapshot-tasks describe-security-groups describe-vpc-peering-connections
describe-instance-attribute describe-snapshot-attribute describe-vpcs
describe-instance-status describe-snapshots describe-vpn-connections
describe-instances describe-spot-datafeed-subscription describe-vpn-gateways
describe-internet-gateways describe-spot-fleet-instances