Python 最新版のインストール(ソースコードを使用)(Windows 上)

Windows で,Python をソースコードからビルドして,インストールする.

Python のインストール: 別ページ »で説明は,複数の方法がある.

関連する外部ページhttps://devguide.python.org/setup/#getting-the-source-code

前準備

Python 3.12 のインストール

インストール済みの場合は実行不要。

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要である。

REM Python 3.12 をシステム領域にインストール
winget install --scope machine --id Python.Python.3.12 -e --silent --accept-source-agreements --accept-package-agreements
REM Python のパス設定
set "PYTHON_PATH=C:\Program Files\Python312"
set "PYTHON_SCRIPTS_PATH=C:\Program Files\Python312\Scripts"
if exist "%PYTHON_PATH%" setx PYTHON_PATH "%PYTHON_PATH%" /M >nul
if exist "%PYTHON_SCRIPTS_PATH%" setx PYTHON_SCRIPTS_PATH "%PYTHON_SCRIPTS_PATH%" /M >nul
for /f "skip=2 tokens=2*" %a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "SYSTEM_PATH=%b"
echo "%SYSTEM_PATH%" | find /i "%PYTHON_PATH%" >nul
if errorlevel 1 setx PATH "%PYTHON_PATH%;%PYTHON_SCRIPTS_PATH%;%SYSTEM_PATH%" /M >nul

関連する外部ページ

Python の公式ページ: https://www.python.org/

Gitのインストール

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。

REM Git をシステム領域にインストール
winget install --scope machine --id Git.Git -e --silent --accept-source-agreements --accept-package-agreements
REM Git のパス設定
set "GIT_PATH=C:\Program Files\Git\cmd"
for /f "skip=2 tokens=2*" %a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "SYSTEM_PATH=%b"
if exist "%GIT_PATH%" (
    echo "%SYSTEM_PATH%" | find /i "%GIT_PATH%" >nul
    if errorlevel 1 setx PATH "%GIT_PATH%;%SYSTEM_PATH%" /M >nul
)

CMakeのインストール

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。

REM CMake をシステム領域にインストール
winget install --scope machine --id Kitware.CMake -e --silent
REM CMake のパス設定
set "GMAKE_PATH=C:\Program Files\CMake\bin"
if exist "%GMAKE_PATH%" (
    echo "%PATH%" | find /i "%GMAKE_PATH%" >nul
    if errorlevel 1 setx PATH "%PATH%;%GMAKE_PATH%" /M >nul
)

Visual Studio のインストール(Windows 上)

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

winget install --scope machine --accept-source-agreements --accept-package-agreements Microsoft.VisualStudio.2022.Community --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core Microsoft.VisualStudio.Component.VC.CLI.Support Microsoft.VisualStudio.Component.CoreEditor Microsoft.VisualStudio.Component.NuGet Microsoft.VisualStudio.Component.Roslyn.Compiler Microsoft.VisualStudio.Component.TextTemplating Microsoft.VisualStudio.Component.Windows11SDK.26100 Microsoft.VisualStudio.Component.VC.Tools.x86.x64 Microsoft.VisualStudio.Component.VC.ATL Microsoft.VisualStudio.Component.VC.ATLMFC Microsoft.VisualStudio.Component.VC.Llvm.Clang Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset Microsoft.VisualStudio.Component.VC.CMake.Project Microsoft.VisualStudio.Component.VC.ASAN Microsoft.VisualStudio.Component.Vcpkg"
winget install --scope machine --accept-source-agreements --accept-package-agreements --silent Microsoft.VCRedist.2015+.x64

インストールされるコンポーネント:

インストール完了の確認

winget list Microsoft.VisualStudio.2022.Community

Python のビルドとインストール

  1. x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)を実行する   (手順:スタートメニュー →Visual Studio 20xx」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」 を選ぶ)。
    「x64 Native Tools コマンドプロンプト」がないときは,ビルドツール (Build Tools) をインストールすると,x64 Native Tools コマンドプロンプトもインストールされる.その手順は,別ページ »で説明している.
  2. 作業ディレクトリとインストールディレクトリを削除する
    mkdir c:\tools
    rmdir /s /q cpython
    
  3. Python のソースコードをダウンロード
    cd c:\
    git clone https://github.com/python/cpython.git
    
  4. インストール手順の説明は PCbuild\readme.txt に記載されている.一読しておく
  5. プロジェクトの再ターゲットの操作

    c:\cpython\PCbuild\pythoncore.vcxproj を開く(右クリックメニューが便利).すると Visual Studio が起動する.

    ソリューションを右クリック.右クリックメニューで「ソリューションの再ターゲット」を選ぶ.

  6. PCbuild\build.bat の実行

    * このとき作業ディレクトリ内に bzip, sqlite3, xz, zlib, libffi, openssl-bin, tcltk が自動でダウンロードが始まる

    cd c:\
    cd cpython
    .\PCbuild\build.bat
    
  7. 終了の確認
  8. 試しに起動してみる
    c:\cpython\PCbuild\amd64\python.exe
    

    exit()」で終わる

  9. pip をインストール

    pip を使うときは,「c:\cpython\PCbuild\win32\python.exe -m pip」のように使う

    curl -O https://bootstrap.pypa.io/get-pip.py
    c:\cpython\PCbuild\amd64\python.exe get-pip.py