Windows での Python 3.8 のインストール,インストール後の設定,Python の各種パッケージのインストール,性能確認の手順をスクリーンショット等で説明する.
【目次】
【外部へのリンク】https://docs.python.org/ja/3/using/windows.html
動画「Python 3.8 のインストール(Windows 上)」の URL: https://www.youtube.com/watch?v=tUItIi5pZ08
すでに,Python がインストール済みのとき.
ここで示すインストール手順とは異なる設定ですでに Python をインストールしていた場合は,それをそのまま使うよりも, アンインストールしておいたほうがトラブルが少ない可能性がある.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
この操作は,必ずPython をすべてアンインストールした後に行うこと.
rmdir /s /q %APPDATA%\Python cd "C:\Program Files" for /F %i in ('dir /ad /b /w Python3*') do rmdir /s /q %i
インストールでの注意点
Windows で,ユーザ名が日本語のとき,あとでトラブルが発生するかもしれない. トラブルの回避策として, Python をシステム管理者の領域にインストール(パソコンの全ユーザの共有領域)する手順をここで説明する.
TensorFlow を使う予定がある場合は,https://pypi.org/project/tensorflow-gpu/#filesで,必要な Python のバージョンを確認しておく. 2022/7 時点では,TensorFlow バージョン 2.9 が動くのは,Python 3.10 または Python 3.9 または Python 3.8 または Python 3.7 (https://pypi.org/project/tensorflow/2.9/#files)
インストール手順
Windows での Python 3.8 のインストール(あとのトラブルが起きにくいような手順を定めている)
ページの上の方にある「Downloads」をクリック. 「Downloads」の下にメニューが出るので,その中の「Windows」をクリック
ここでは,Python 3.8.x (x は数字)を探して,選ぶ.
以下,Python 3.8.7 を選んだとして説明を続ける.他のバージョンでも以下の手順はだいたい同じである.
TensorFlow を使う予定がある場合は,https://pypi.org/project/tensorflow-gpu/#filesで,必要な Python のバージョンを確認しておく. 2022/7 時点では,TensorFlow バージョン 2.9 が動くのは,Python 3.10 または Python 3.9 または Python 3.8 または Python 3.7 (https://pypi.org/project/tensorflow/2.9/#files)
Windows の 64ビット版のインストーラをダウンロードしたいので、「Windows Installer (64-bit)」を選ぶ
※ すでに Python ランチャーをインストール済みのときは, 「Install launcher for all users (recommended)」がチェックできないようになっている場合がある.そのときは,チェックせずに進む.
「Install for all users」を選ぶ理由.
ユーザ名が日本語のときのトラブルを防ぐため.
そして,Python のインストールディレクトリは,「C:\Program Files\Python38」のように自動設定されることを確認.
さきほど、「Add Python ... to PATH」をチェックしたので、 Python についての設定が自動で行われたことを確認する
py とpip にパスが通っていることの確認である.
where py where pip
where py では「C:\Windows\py.exe」 が表示され, where pip では「C:\Program Files\Python38\Scripts\pip.exe」 が表示されることを確認. (「38」のところは使用する Python のバージョンに読み替えること).
※ エラーメッセージが出ないことを確認.
python --version
python
下の実行例では、バージョン番号として「1928」が表示されている
下の実行例では、バージョン番号として「14.2」が表示されている
from distutils.msvc9compiler import * get_build_version()
Python のパッケージも同時にインストールされることが分かる.
※ エラーメッセージが出ないことを確認.
pip list
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools
※ エラーメッセージが出ないことを確認.
pip list
python -m pip install -U pip setuptools numpy pandas matplotlib seaborn scikit-learn scikit-learn-intelex
python import numpy as np print(np.sin(0)) exit()
Python 開発環境,Python コンソール(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, spyder)のインストールを行う.
Python, pip, Python 開発環境,Python コンソールのコマンドでの起動のまとめ.
コマンドプロンプトで次のコマンドで起動、多くはスタートメニューでも起動できる
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
Python の隔離された環境を使用したいときは,次のような手順で, venv を用いて,Python の隔離された環境を作る.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
Python の使用は「C:\venv\py36\Scripts\activate.bat」の後,「python」で行う.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
pip install -U numpy matplotlib
新しくコマンドプロンプトを開き,次のコマンドを実行. Jupyter Qt Console が開けば OK.
jupyter qtconsole
import numpy as np %matplotlib inline import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') # Suppress Matplotlib warnings x = np.linspace(0, 6, 100) plt.style.use('default') plt.plot(x, np.sin(x))
新しくコマンドプロンプトを開き,次のコマンドを実行. ntetact が開けば OK.
jupyter nteract
そのために「Start a new notebook」の下の「Python」をクリック,次のプログラムを入れ実行.
import numpy as np %matplotlib inline import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') # Suppress Matplotlib warnings x = np.linspace(0, 6, 100) plt.style.use('default') plt.plot(x, np.sin(x))
jupyter notebook --generate-config
c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
"jupytext": {"formats": "ipynb,py"}
行列の積, 主成分分析, SVD, k-Means クラスタリングを実行し,性能を確認する.
コマンドプロンプトを管理者として実行: 別ページで説明している.
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) # 行列の積 a = time.time(); Z = numpy.dot(X, Y); print(time.time() - a) # 主成分分析 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)
実行結果の例
エラーとして,「RuntimeError: The current Numpy installation ('C:\\Program Files\\Python38\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime. See this issue for more information: https://tinyurl.com/y3dm3h86」のようなメッセージが出た場合.
次のように操作して,エラーを回避.