Skip to content

Instantly share code, notes, and snippets.

@seraphy
Created March 27, 2020 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraphy/8a2b94b4de996cabed9cd4c9d8c795e4 to your computer and use it in GitHub Desktop.
Save seraphy/8a2b94b4de996cabed9cd4c9d8c795e4 to your computer and use it in GitHub Desktop.
2019年2月時点での、13インチの白MacBook 2007 Lateに、Git + Python3 + Jupyter + Sphinx でPython3勉強環境を作るまでの手順を示す。

概要

2019年2月時点での、13インチの白MacBook 2007 Lateに、Git + Python3 + Jupyter + Sphinx でPython3勉強環境を作るまでの手順を示す。

このマシンは2007年に購入したもので、その後、アップデートやクリーンインストールを経て、数年前まではhomebrewとdockerが動いていたが、いまでは動かなくなっている。

環境を作るのに苦労したので備忘録として残す。(次は、もう無理じゃないかという予感がある...)

これはadhocに調査・試行した履歴であり、抜け漏れ考慮不足、認識誤り等々あると思うが、今後のために備忘録として残すものである

要点

  1. すでにOSが古く、新しいライブラリ、ツールは使えない。よって、OSX10.7.5 Lionをサポートしていた当時(2015年より前)の、古いバージョンを探して入れる。(動かなくなったツールは消しておく)
  2. ただし世のサイトがTLSv1.1をサポートしなくなったので、HTTPS通信するためにTLSv1.2をサポートするOpenSSLとCurl, Gitを入れる。
  3. Python3は公式パッケージから入れれば、opensslも自前のtls1.2対応のものが入る。

前提

このマシンにはChrome, Firefoxなどのブラウザは、すでにLion対応の最終バージョンが入っている。(いまから、これらの最終バージョンを入手する方法は調べていない。)

手順

  • HomeBrewではなくTigerBrewを使う

  • OpenSSL更新

  • Curl更新

  • Git導入

  • TigerBrew更新

  • python2更新

  • ruby更新

  • brewからpython3導入試行→失敗

    • Sphinx入れる
    • gcc入れる
    • clang入れる
  • 公式からpython3のosx10.6対応版入れる

    • python3の証明書入れる
  • venvで環境作る

    • pipの更新
    • numpy==1.10.1
    • pandas==0.17.1
    • scipy==0.16.1
    • seaborn, matplotlib
    • jupyter入れる
    • 起動シェル作成する
  • Sphinx入れる

    • IPAフォント入れる
    • sphinxcontrib-seqdiag入れる
    • Pillow==2.7.0

以上。

TigerBrewを導入する

TigerBrew はHomeBrewからフォークした、Tigerで使えるBrewであり、このプロジェクト自身すでに止まっているので、ここから得られるツールは古いものばかりである。(なのでLionにも入るはず)

※ すでに入っているHomeBrewは /usr/local下のBrew関連を消しておく

ruby -e "$(curl -fsSkL raw.github.com/mistydemeo/tigerbrew/go/install)"

OpenSSLを更新する

TLSv1.2以降をサポートする必要が多々あるので、まずはOpenSSLを更新する。

brew install openssl

現状openssl 0.9.8 から 1.0.2j にあがる。

curlを更新する

先に導入したOpenSSLを使うようにcurlを入れる。

brew install --with-openssl curl
brew link curl --force

導入前は、

curl 7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8z zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM SSL libz 

導入後は、以下のようになる

curl 7.50.3 (x86_64-apple-darwin11.4.2) libcurl/7.50.3 OpenSSL/1.0.2j zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 

gitを入れる

上記のOpenSSLとCurlを使うようにしてgitを入れる。

(brew自体がgitを使っているので、gitの更新が必要になる。)

brew install --force --with-brewed-curl --with-brewed-openssl git

バージョンは以下のようになった。

git version 2.10.2

TigerBrewのリポジトリを更新

TigherBrewの公式ページの案内にしたがって、Brewのリポジトリを更新する。

(これで古いバージョンを探すようになる。)

cd `brew --repository`
git remote set-url origin https://github.com/mistydemeo/tigerbrew.git
git fetch origin
git reset --hard origin/master

brewのバージョン確認は以下のようになった。

$ brew -v
Homebrew 0.9.5 (git revision 30229; last commit 2018-10-06)

Python2の更新

Python2もTigerBrewで更新できる。

先と同様に更新したOpenSSLを使うように指示しておく。

brew link xz pkg-config makedepend openssl
brew install python --with-brewed-openssl

Python 2.7.1 から Python 2.7.15 になった。

Rubyの更新

RubyもTigerBrewで更新できる。

先と同様に更新したOpenSSLを使うように指示しておく。

brew install ruby --with-brewed-openssl 

導入前は、

ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin11.0]

導入後は、以下のようになった。

ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin11.0]

TigherBrewでPython3のインストールの試行 → 失敗

TigerBrewのカタログにPython3があるので、brew install python3 で入ると思いきや、Sphinx-doc がないといわれる。この Sphinx-doc が、そもそもTigerBrewのカタログにないので、どうにもならない。

ためしに

pip install --upgrade pip
pip install -U Sphinx

でSphinxを入れてみたが、brewは動かない。

https://github.com/mistydemeo/tigerbrew/blob/master/Library/Formula/python3.rb

からpython3のFormulaをダウンロードして、依存関係からSphinxを消して試してみると、 今度は、clangでこける。

gcc, llvmをインストールしてみるが、状況に変わりなし。

念のため、インストール後の状態の確認

$ gcc -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

$ clang -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix

$ gcc-7 --version
gcc-7 (Tigerbrew gcc 7.3.0) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


LLVM executables are installed in /usr/local/opt/llvm/bin.
Extra tools are installed in /usr/local/opt/llvm/share/llvm.

python3はどうやってもtigherbrewでは入れられなかった。

pyenvによるPython3.5の導入

あとで気づいたが、pyenv を入れられるので、ここからpython3を入れられたようだ。(実際の手順としては先に公式のPython3.6.4を入れてから試した)

brew install pyenv
brew install pyenv-virtualenv

.bash_profile に追記して、pyenvにPATH等を通す。

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

Python3.5

pyenvのリストで確認すると、入れられる最終バージョンは3.5.0だった。

pyenv install 3.5.0

環境を作ってみる

$ mkdir -p work/pyenv35
$ cd work/pyenv35
$ pyenv local 3.5.0
$ python --version
Python 3.5.0

Python公式サイトからPython3を入れる

tigerbrewでpython3のインストールができなかったので、考えてみればbrewにこだわる必要もないし公式サイトからpython3を入れることにした。

https://www.python.org/downloads/mac-osx/

python-3.6.4-macosx10.6.pkg がosx10.7.5対応の最終のようだ。

https://www.python.org/downloads/release/python-364/

インストール後のバージョンの確認

$ python3 --version
Python 3.6.4

証明書の導入

パッケージで入れたPython3は、そのままではHTTPSで通信できない。

(Python3が独自にOpenSSLをもっていてシステムのOpenSSLを使わないため。)

https://qiita.com/orangain/items/0a641d980019fd7e0c52

付属コマンドで証明書をマシンに導入する。

open /Applications/Python\ 3.6/Install\ Certificates.command

動作確認してみる

(py36env) $ python -c "import ssl;print(ssl.OPENSSL_VERSION)"
OpenSSL 1.0.2m  2 Nov 2017

(上記例はpy36envという環境をactivateしているのでpythonでpython3が起動している)

venvによる環境作成とパッケージ導入

環境を作成してみる

python3 -m venv py36env
cd py36env
source ./bin/activate

pipの更新

jupyterの学習環境をつくるにあたり、以下のパッケージを入れようと思う

  • numpy
  • scipy
  • pandas
  • seaborn
  • matplotlib

まずはpipを更新しておく

pip3 install --upgrade pip

numpy==1.10.1 を入れる

numbyはネイティブライブラリを使っている。

新しいnumpyはOSX10.9以降のAPIを使っていたりするので当然OSX10.7.5では動作しない。

Lionで動作しそうなバージョンを日付で適当に選んでみる。

  • 2015/10/13版
pip install numpy==1.10.1

pandas==0.17.1

numpyと同様な理由で古いバージョンを明示的に入れる必要がある。

  • 2015/11/21版
pip install pandas==0.17.1

scipy==0.16.1

numpyと同様な理由で古いバージョンを明示的に入れる必要がある。

  • 2015/10/25版
pip install scipy==0.16.1

seaborn, matplotlibを入れる

seabornとmatplotlibは、それ自身は古いバージョンは指定しなくても良いようだ。

pip install seaborn
pip install matplotlib

Jupyterを入れる

pip install jupyter

Jupyterのランチャを作成する

py36envに入ってjupyter notebookと毎度入力するのは大変なので、 以下のようなAppleScriptを作成する。

tell application "Terminal"
	do script "cd $HOME;. $HOME/py36env/bin/activate;jupyter notebook --notebook-dir=Documents/jupyter-notebook&"
end tell

このスクリプトを Py36EnvJupyterLauncher.app など適当な名前でアプリケーションバンドルとして保存する。

これを実行すると

  • ターミナルを開いて、py36env環境に入り
  • jupyter notebookを所定のフォルダをルートとして起動し
  • py36env環境でターミナルの待ちうけ状態にはいる (pipで追加のライブラリを入れやすいように)

といった一連の動作を行う。

jupyterの動作確認

import seaborn
titanic = seaborn.load_dataset('titanic')
titanic.describe()
%matplotlib inline

import pandas

plot_df = pandas.DataFrame({
    'col1': [1, 3, 2, 4],
    'col2': [3, 4, 5, 2],
    'col3': [4, 2, 3, 5],
})
plot_df.plot()

https://matplotlib.org/gallery/lines_bars_and_markers/simple_plot.html

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

一応、動作するようだ。

ひとまず、これで JupyterによるPython3学習環境はできたようだ。

Sphinxによるドキュメント作成環境

ついでにSphinxの環境も作っておく。

Sphinxのインストール

py36環境に入って、Sphinx公式ページにしたがって、pip でインストールする。

http://www.sphinx-doc.org/ja/stable/index.html

. py36env/bin/activate
pip install -U Sphinx

IPAexフォントを入れる

ブロックダイアグラムのプラグインを利用して、シーケンス図を生成したい。

参考 → https://qiita.com/kinpira/items/505bccacb2fba89c0ff0

画像として出力されるフォントに日本語のIPAexフォントを使いたいので、まず、IPAexフォントを入れる。

https://ipafont.ipa.go.jp/#jp

unzip IPAexfont00301.zip 

展開して、これを

/Library/Fonts/ipaexg.ttf にコピーする。

sphinxcontrib-seqdiagとpillowを入れる

ブロックダイアグラムの画像を生成するプラグインを導入する。

http://blockdiag.com/ja/seqdiag/sphinxcontrib.html

pip install Pillow==2.7.0
pip install sphinxcontrib-seqdiag

画像生成で Pillow を使っているが、これもネイティブライブラリを使うので 古いバージョン(==2.7.0)でないと動作しない。

HTMLの生成をテストしてみる

適当なディレクトリを作成して

sphinx-quickstart でひな形を作成する。

seqdiagの設定をconf.pyに加える

extensions = [
    'sphinx.ext.autodoc',
    'sphinxcontrib.seqdiag',
]

seqdiag_fontpath = '/Library/Fonts/ipaexg.ttf'

拡張モジュールの指定とフォントパスの指定を行う。

これで

###########################
 sphinx seqdiag
###########################

.. seqdiag::
   :desctable:

   seqdiag {
      ブラウザ -> ウェブサーバ[label = "リクエスト"];
      ウェブサーバ -> データベース[label = "クエリー"];
      ブラウザ [description = "Internet Explorer 11 (32bit)"];
      ウェブサーバ [description = "Apache Tomcat 8.5"];
      データベース [description = "Oracle 12c"];
   }

といったrstファイルが make html でビルドできれば成功である。

(ラベルに日本語を使っているので、フォントのパスが通っていないと変換時にエラーになる。)

シーケンス図以外にも

も、同様な手順で導入できる。

結論

結局のところ、Python3に関して言えば、brewをいれずとも、公式のPython3.6.4インストーラだけで行けたようだ。

Python3が動作すれば、各種ライブラリは pip で明示的に古いバージョンを指定すれば、たいてい、なんとかなるっぽい。

ただし、pipで新しいライブラリをインストールするときに依存するライブラリのバージョンが更新されることが多々あるので、うっかり上がっていないか都度注意する必要がある。

gitを使うのであれば、tigerbrewを使って、openssl, curl, gitを導入できる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment