差分

提供: fukudat.net
ナビゲーションに移動検索に移動
3,680 バイト追加 、 2021年8月12日 (木) 02:15
pythonの実行環境を制御するコマンド.システム全体 (global) に使用するバージョンと,個別のディレクトリ (local) に使用するバージョンを設定・変更することができる.
=== Install by brew ===<pre>$ brew install pyenv-virtualenv</pre>
=== Install by git ===
<pre>
$ brew install git clone https://github.com/pyenv/pyenv.git ~/.pyenv$ git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
</pre>
=== Configure ===PATHを設定するために,~/.bash_profile or ~/.zprofile に以下を追加.
<pre>
export PYENV_ROOT=${HOME}/.pyenv
if [ -d "${PYENV_ROOT}" ]; then
export PATH=eval "${PYENV_ROOT}/shims:$PATH(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
$ pyenv install 3.6.3
</pre>
 
トラブルシューティング:
* [[/pyenv/High Sierra でのpython インストール失敗問題 ]]
==== Python バージョンを表示 ====
$ cd python2
$ pyenv local 2.7.10
</pre>
 
== tkinter ==
これがないとvisualization系のパッケージが動かない。
 
=== Install ===
まず、前提になっている brew で tcl-tk をインストールする。
<pre>
$ brew install tcl-tk
</pre>
 
次に、pyenvを brewでインストールした場合は /usr/local/Cellar/pyenv/VERSION/plugins/python-build/bin/python-build を
pyenv を git でインストールした場合は ~/.pyenv/plugins/python-build/bin/python-build を次のように編集。
<pre>
--- old/plugins/python-build/bin/python-build
+++ new/plugins/python-build/bin/python-build
@@ -779,7 +779,7 @@ build_package_standard_build() {
export CC=clang
fi
${!PACKAGE_CONFIGURE:-./configure} --prefix="${!PACKAGE_PREFIX_PATH:-$PREFIX_PATH}" \
- $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
+ $CONFIGURE_OPTS --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
) >&4 2>&1
{ "$MAKE" $MAKE_OPTS ${!PACKAGE_MAKE_OPTS} "${!PACKAGE_MAKE_OPTS_ARRAY}"
</pre>
つまり、$CONFIGURE_OPTS で始まる1行に <tt>--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'</tt> を加える。
ただし、M1 Macの場合は、/usr/local/opt の部分が /opt/homebrew/opt に置き換わる。
 
この設定をした上で、pyenv で python 自体を再インストールする。
<pre>
$ pip freeze > requirements.txt. # 現在インストールされているパッケージを覚えておく
$ pyenv versions
system
3.8.11
* 3.9.6 (set by /Users/YOU/.pyenv/version)
$ pyenv uninstall 3.9.6
$ pyenv install 3.9.6
$ pip install -r requirements.txt # 消えちゃったパッケージをインストールし直す
</pre>
 
これを、使用するすべての python version で繰り返す。
 
== matplotlib ==
 
グラフを表示するライブラリ.
 
=== Install ===
<pre>
$ pip install matplotlib
</pre>
 
=== グラフ描画の例 ===
 
<pre>
import math
import numpy as np
from matplotlib import pyplot
 
x = np.linspace(0, 2 * math.pi, 100)
y = np.sin(x)
 
pyplot.plot(x, y)
pyplot.show()
</pre>
 
もし以下のようなエラーが出たら・・・
<pre>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
</pre>
 
matplotlibrc を編集して,
<pre>
backend: macosx
</pre>
とある行を,
<pre>
backend: Tkagg
</pre>
に書き換える.
 
matplotlibrc の在りかは,次のコマンドで確認できる.
<pre>
$ python -c "import matplotlib;print(matplotlib.matplotlib_fname())"
</pre>
 
参考文献: https://qiita.com/Kodaira_/items/1a3b801c7a5a41c9ce49
 
== opencv ==
 
=== Install ===
brew でライブラリをインストール
<pre>
$ brew install opencv
</pre>
 
pip で opencv-python をインストール
<pre>
$ pip install opencv-python
</pre>
 
パッケージとしては、cv2 をインポート
<pre>
#!/usr/bin/env python
import cv2
...
</pre>

案内メニュー