pyenvは,システム Python とは別の Python をインストールするのに便利なツールである.
Python の隔離された環境の作成と利用が簡単にできる機能もある.
ここで行うこと.
pyenv をインストールする.pyenv の配下に Python 3.8, Python 3.7, Python 3.6 をインストールする.
※ このページの手順は,普通の Ubuntu 20.04 と, WSL2 上の Ubuntu 20.04 の両方で確認済み.
【目次】
Ubuntu で OS のシステム更新を行うときは, 端末で,次のコマンドを実行する.
Ubuntu のインストールは別ページ »で説明
sudo apt -y update sudo apt -yV upgrade sudo /sbin/shutdown -r now
pyenv は Python のインストールが簡単にできる機能などを持つソフトウェア.
rm -rf ~/.pyenv
cd /tmp git clone https://github.com/pyenv/pyenv.git ~/.pyenv cd ~/.pyenv git pull src/configure make -C src
echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.profile echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.profile echo ' export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.profile echo ' eval "$(pyenv init --path)"' >> ~/.profile echo 'fi' >> ~/.profile echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.profile exec $SHELL -l source ~/.profile
sudo apt -y update sudo apt -y install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Ubuntu 20.04 の場合
which python3 python3 --version
WSL2 上の Ubuntu 20.04 の場合
which python3 python3 --version
pyenv rehash pyenv install -l
pyenv install -l | grep 3.8
pyenv install 3.8.7
pyenv shell 3.8.7 python -m pip install -U pip setuptools
Python の切り替えは次のようなコマンドで行う.
デフォルトで pyenv 配下の Python 3.8.7 を使いたいときは, 次のように設定する
echo 'pyenv shell 3.8.7' >> ~/.bashrc exec $SHELL -l
pyenv shell 3.8.7 python --version python print(1 + 2) exit()
「pyenv shell ・・・」でうまく Python のバージョンが切り替わらない場合には「source ~/.bashrc」を実行してから,もう一度上を試す
ls ~/.pyenv/versions
which pip which easy_install
pyenv shell 3.8.7 which pip pip list
pyenv shell 3.8.7 python -m pip install -U pip setuptools
次のように実行する.
pyenv shell 3.8.7 python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
JupyterLab の起動は,「pyenv shell <バージョン>; jupyter lab」. Jupyter Qt Console の起動は,「pyenv shell <バージョン>; jupyter qtconsole」.
次のように実行する.
pyenv shell 3.8.7
※ トラブルの可能性を減らすために,次の操作でアンインストールを行っておく.
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow-intel tensorflow-text tensorflow-estimator tf-models-official tf_slim tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer
※ 「pip3 install」は,Python パッケージをインストールするためのコマンド.
python -m pip install -U pip setuptools sudo apt -y install libopencv-dev libopencv-core-dev python3-opencv python -m pip install -U tensorflow==2.10.1 tf_slim tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer numpy pillow pydot matplotlib seaborn pandas scipy scikit-learn scikit-learn-intelex opencv-python opencv-contrib-python python -m pip install git+https://github.com/tensorflow/docs python -m pip install git+https://github.com/tensorflow/examples.git python -m pip install git+https://www.github.com/keras-team/keras-contrib.git
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import numpy; print( numpy.__version__ )"
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import tensorflow as tf; print( tf.__version__ )"
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import keras; print( keras.__version__ )"
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
実行結果の中に,次のように「device_type: "GPU"」があれば,GPUが認識できている.エラーメッセージが出ていないことを確認しておくこと.
次のように実行する.
pyenv shell 3.8.7
sudo apt -y update sudo apt -y install libmecab-dev source ~/py38/bin/activate python -m pip install -U pip setuptools python -m pip install -U numpy scipy h5py scikit-learn scikit-learn-intelex scikit-image seaborn pandas pillow pytest cython bokeh statsmodels plotly sympy csvkit docopt pyproj flake8 protobuf bs4 html5lib rope wrapt cffi wheel six sphinx bottleneck pygments numexpr xlrd xlsxwriter lxml graphviz pydot flask django redis pylint bz2file PyOpenGL msgpack mecab ggplot matplotlib-venn pyglet pygame cocos2d bottle rtree shapely fiona geopandas geopy geographiclib requests \ pandasql pyyaml bokeh pymc3 mkl mkl-include holoviews pandas-bokeh ggplot prettyplotlib pybrain3 firebase-admin googletrans google-cloud-vision gpyocr azure-cognitiveservices-vision-computervision gensim gloo scikit-video scikits.datasmooth scikits.example scikits.fitting scikits.optimization scikits.vectorplot zodb gdata pyr2 pycaret python -m pip install -U gdal
sudo apt -y update sudo apt -y install swig
http://www.swig.org/Doc1.3/Python.html#Python_nn6 に記載のサンプルプログラム
%module example %{ #define SWIG_FILE_WITH_INIT #include "example.h" %} int fact(int n);
#include "example.h" int fact(int n) { if (n < 0){ /* This should probably return an error, but this is simpler */ return 0; } if (n == 0) { return 1; } else { /* testing for overflow would be a good idea here */ return n * fact(n-1); } }
int fact(int n);
#!/usr/bin/env python """ setup.py file for SWIG example """ from distutils.core import setup, Extension example_module = Extension('_example', sources=['example_wrap.c', 'example.c'], ) setup (name = 'example', version = '0.1', author = "SWIG Docs", description = """Simple swig example from docs""", ext_modules = [example_module], py_modules = ["example"], )
swig -python example.i python setup.py build_ext --inplace ls -la example.* build
import example example.fact(4) exit()
行列の積, 主成分分析, SVD, k-Means クラスタリングを実行し,性能を確認する.
Python 3.6 での実行結果を示す.
pyenv shell 3.6.12 python -m pip install -U numpy scikit-learn scikit-learn-intelex
import time import numpy import numpy.linalg import sklearn.decomposition import sklearn.cluster X = numpy.random.rand(2000, 2000) Y = numpy.random.rand(2000, 2000) # matrix product a = time.time(); Z = numpy.dot(X, Y); print(time.time() - a) # pca pca = sklearn.decomposition.PCA(n_components = 2) a = time.time(); pca.fit(X); X_pca = pca.transform(X); print(time.time() - a) # SVD a = time.time(); U, S, V = numpy.linalg.svd(X); print(time.time() - a) # k-means a = time.time(); kmeans_model = sklearn.cluster.KMeans(n_clusters=10, random_state=10).fit(X) labels = kmeans_model.labels_ print(time.time() - a)
実行結果の例