LoginSignup
11
13

More than 5 years have passed since last update.

MacにPythonのインストール

Last updated at Posted at 2016-10-29

Pythonをインストールする前に準備することがあります。

Homebrew

Homebrewをインストールします。

rubyはMacOSにインストールされていますので、以下のコマンドをターミナルから実行します。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

インストール先を確認しましょう。

$ which brew

/usr/local/bin/brew

Homebrewをアップデートするには以下のコマンドを実行します。

$ brew update && brew upgrade

pyenv

次にPyenvをインストールします。これはPythonのバージョン管理ソフトウエアーです。
通常MacOSにはPython2.7がインストール済みですが、複数のバージョンのPythonを利用する場合には、このソフトウエアーを利用します。

$ brew install pyenv

その後、自分のホームディレクトリーにある.bash_profileを編集します。

viで編集するか、

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

以下のように.bash_profileに書き込みます。

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

その後、

$ source .bash_profile

でリフレッシュします。

これで、Pythonをインストールする準備は整いました。

それでは、実際にインストールしてみましょう。
インストール可能なPythonの一覧は以下のコマンドで確認できます。

$ pyenv install -l  
Available versions:  
  2.1.3  
  2.2.3  
  2.3.7  

特定のバージョンをインストールします。

$ pyenv install 3.5.1

インストールされたバージョンを確認します。

$ pyenv versions  
* system (set by /Users/yoshi/.pyenv/version)  
  3.5.1  

実際アクティブにするバージョンを選択するには、

$ pyenv global 3.5.1

とします。確認してみると、

$ pyenv versions  
  system  
* 3.5.1 (set by /Users/yoshi/.pyenv/version)  
のように*印が変更されています。  

これで3.5.1のバージョンをインストールできました。

確認してみましょう。

$ which python

/Users/ユーザ名/.pyenv/shims/python

自分のホームディレクトリーの.pyenvにインストールされています。

それでは実際に起動してみます。

$ python  
Python 3.5.1 (default, Jul  5 2016, 01:34:21)  
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.  
\>>>  
11
13
1

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
11
13