ウェブサイト検索

Debian 11/Debian 10 に Python 3.10 をインストールする |


Debian 11/Debian 10 Linux システムに Python 3.10 をインストールする方法に関するこのガイドへようこそ。 Python 3.10 は Python の最新リリース バージョンですが、以前のバージョンの Python 3.9 と比べると安定していません。最終候補はまだ完成していません。 Python は広く使用されているオープンソースのオブジェクト指向プログラミング言語の 1 つであり、そのシンプルで学習しやすい構文により人気が高まっています。 Python は次の分野で使用されます。

  • ビッグデータを処理し、複雑な数学を実行します。
  • データベース システムに接続し、ファイルを読み取り、変更します。
  • ラピッドプロトタイピングまたは実稼働対応のソフトウェア開発。
  • ワークフローを作成するためにソフトウェアと併用します。
  • Web アプリケーションの作成。

最新リリース バージョンの Python 3.10 には、次の優れた機能が付属しています。

  • 構造パターンマッチング
  • 括弧内のコンテキストマネージャー
  • エラーメッセージの改善
  • デバッグ用の正確な数値によるインタプリタの改善
  • X|Y としての共用体型、パラメータ仕様変数などを許可する型指定機能に関するさらなる更新
  • セキュリティアップデートなど

Python 3.10 の新機能を理解したので、それを Debian システムにインストールしてみましょう。

インストールの開始

このガイドでは、次のものが必要です。

  1. Debian 11|10 オペレーティング システムがインストールされている
  2. sudo 権限を持つユーザー。

システムをアップデートしてください。

sudo apt update && sudo apt upgrade -y

Debian 11/Debian 10 に Python 3.10 をダウンロード

このガイドでは、ソース コードからビルドして Python 3.10 を Debian 11|10 にインストールします。この方法の利点の 1 つは、Python の利用可能な最新バージョンが保証されていることです。

ソースから Python 3.10 をビルドできるようにするには、まず必要な依存関係をインストールする必要があります。

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

次に、公式 Python リリース ページから Python 3.10 をダウンロードします。このページにアクセスしているときに、Python 3.10 のGzippeed tarball をダウンロードします。あるいは、ダウンロード リンクを取得し、次のように Wget でプルします。

wget https://www.python.org/ftp/python/3.10.12/Python-3.10.12.tgz

tarballをダウンロードしたら、以下のように解凍します。

tar -xf Python-3.10.*.tgz

抽出したディレクトリに移動し、configure コマンドを実行して、必要な依存関係が利用可能かどうかを確認します。コマンドでは、–enable-optimizations フラグを使用してバイナリを最適化し、複数のテストを実行します。

cd Python-3.10.*/
./configure --prefix=/usr/local --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

サンプル出力:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.10... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
.........
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile

チェックが完了したら、以下のようにソースからPython 3.10をビルドします。 -j フラグを使用してプロセスを高速化することを忘れないでください。これは、システム内のコアの数を指定します。 nproc コマンドはシステム コアを表示します。

make -j $(nproc)

サンプル出力:

.................
changing mode of build/scripts-3.10/pydoc3 from 644 to 755
changing mode of build/scripts-3.10/idle3 from 644 to 755
changing mode of build/scripts-3.10/2to3 from 644 to 755
renaming build/scripts-3.10/pydoc3 to build/scripts-3.10/pydoc3.10
renaming build/scripts-3.10/idle3 to build/scripts-3.10/idle3.10
renaming build/scripts-3.10/2to3 to build/scripts-3.10/2to3-3.10
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -fno-semantic-interposition -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-use -fprofile-correction -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.cgcc -pthread   -fno-semantic-interposition  -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.10.a -lcrypt -lpthread -ldl  -lutil -lm   -lm 
make[1]: Leaving directory '/home/klinsmann/Python-3.10.12'

make が完了したら、次のように続行して Python 3.10 を Debian 11|10 にインストールします。

sudo make altinstall

altinstall フラグは、デフォルトの Python バイナリ パスを /usr/bin/python に維持するために使用されます。

出力:

Looking in links: /tmp/tmpjgyu604w
Processing /tmp/tmpjgyu604w/setuptools-57.4.0-py3-none-any.whl
Processing /tmp/tmpjgyu604w/pip-21.2.3-py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /usr/local/include/python3.10/setuptools
  sysconfig: /home/klinsmann/Python-3.10.12/Include/setuptools
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /usr/local/include/python3.10/pip
  sysconfig: /home/klinsmann/Python-3.10.12/Include/pip
Successfully installed pip-21.2.3 setuptools-57.4.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

インストールを確認します。

$ python3.10 --version
Python 3.10.12

Python モジュールのインストール | 拡張機能

拡張機能とモジュールは、Python アプリケーションに機能を追加するため、Python では不可欠です。これらの拡張機能とモジュールは、次のコマンドを使用してインストールされる Python パッケージ マネージャー (PIP) を使用して Debian 11|10 にインストールできます。

pip3.10 install --user --upgrade pip

PIP では、構文を使用して選択したモジュールを簡単にインストールできます。

sudo pip3.10 install module-name

beautifulsoup4 というモジュールを使用してこれを実証してみましょう。

sudo pip3.10 install beautifulsoup4

サンプル出力:

Collecting beautifulsoup4
  Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 259 kB/s 
Collecting soupsieve>1.2
  Downloading soupsieve-2.2.1-py3-none-any.whl (33 kB)
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.10.0 soupsieve-2.2.1

Python 仮想環境を作成する

Python 仮想環境は、Python インタープリター、ライブラリ、およびスクリプトがインストールされる場所です。 Python が Debian 11|10 システムに正しくインストールされていることを確認するために、簡単な Python プロジェクトを作成してみましょう。

プロジェクトディレクトリを作成します。

mkdir ~/sample_app && cd ~/sample_app

ディレクトリ内で、sample_app の仮想環境を作成します。

python3.10 -m venv sample_app_venv

次のステップは、環境をアクティブ化することです。

source sample_app_venv/bin/activate

サンプル出力:

root@debian:~/sample_app# source sample_app_venv/bin/activate
(sample_app_venv) root@debian:~/sample_app# 

出力から見ると、現在はシェル内にあり、アプリの名前が接頭辞として付いています。コマンドを使用して仮想環境を終了します。

deactivate

結論

これでこのガイドは終わりです。 Debian 11|10 に Python 3.10 をインストールし、モジュールをインストールし、仮想環境を作成する方法を説明しました。これがあなたにとって有意義なものであったことを願っています。

続きを見る:

  • Debian 11/Debian 10 に Python 3.9 をインストールする
  • CentOS 8/CentOS 7 に Python 3.9 をインストールする
  • Ubuntu に Virtualenv を使用して Python 2 をインストールする方法

関連記事: