Skip to main content

フレキシブル Python ランタイム機能の使用

フレキシブル Python ランタイム機能の概要

組み込み Python を実行する際、InterSystems IRIS はユーザがオペレーティング・システムの既定バージョンの Python を使用していると想定します。しかし、Python の新しいバージョンにアップグレードしたり、Anaconda のような代替ディストリビューションに切り替えたい場合もあります。フレキシブル Python ランタイム機能により、InterSystems IRIS の組み込み Python でこれらのバージョンの Python を使用することができます。

Note:

フレキシブル Python ランタイム機能は、すべてのオペレーティング・システムでサポートされるわけではありません。この機能をサポートするプラットフォームの完全なリストは、"その他のサポート対象機能" を参照してください。

フレキシブル Python ランタイム機能を使用するための準備は、次の 3 つの基本ステップで構成されます。

  1. 使用するバージョンの Python をインストールします。

  2. PythonRuntimeLibrary 構成設定を設定して、組み込み Python を実行する際に使用する Python ランタイム・ライブラリの場所を指定します。

    例 : /usr/lib/x86_64-linux-gnu/libpython3.11.so.1

    この場所は、オペレーティング・システム、Python バージョン、およびその他の要素によって異なります。ここに示す例は、x86 アーキテクチャでの Ubuntu 22.04 上の Python 3.11 の場合です。

    詳細は、"PythonRuntimeLibrary" を参照してください。

  3. 組み込み Python の sys.path 変数に、Python パッケージのインポートに必要な正しいディレクトリが含まれていることを確認します。

    "組み込み Python と sys.path" を参照してください。

組み込み Python と sys.path

組み込み Python を起動すると、これは sys.path 変数に含まれるディレクトリを使用して、インポートする Python パッケージを見つけます。

オペレーティング・システムの既定バージョンの Python で組み込み Python を使用する場合、sys.path には既に、以下のような正しいディレクトリが含まれています。

  • <installdir>/lib/python (InterSystems IRIS 用に予約された Python パッケージ)

  • <installdir>/mgr/python (ユーザがインストールした Python パッケージ)

  • 既定の Python バージョンのグローバル・パッケージ・リポジトリ

    例 : /usr/local/lib/python3.10/dist-packages

    この場所は、オペレーティング・システム、Python バージョン、およびその他の要素によって異なります。ここに示す例は、Ubuntu 22.04 での Python 3.10 の場合です。

Ubuntu 22.04 上の既定バージョンの Python (3.10) では、組み込み Python の sys.path は以下のようになります。

USER>do ##class(%SYS.Python).Shell()

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type quit() or Ctrl-D to exit this shell.
>>> import sys
>>> sys.path
['/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/InterSystems/IRIS/lib/python', 
'/InterSystems/IRIS/mgr/python', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', 
'/usr/lib/python3.10/dist-packages']

Important:

sys.path に、/home/<user>/.local/lib/python3.10/site-packages などのホーム・ディレクトリ内のディレクトリが含まれる場合、ローカル・パッケージ・リポジトリにパッケージがインストールされていることを示している可能性があります。例えば、--target 属性を指定せずに (および sudo を使用せずに) パッケージをインストールする場合、Python はホーム・ディレクトリ内のローカル・パッケージ・リポジトリにパッケージをインストールします。他のユーザがパッケージをインポートしようとすると、失敗します。

インターシステムズでは --target <installdir>/mgr/python オプションを使用することを推奨していますが、sudo を使用して --target 属性を省略してパッケージをインストールすると、パッケージはグローバル・パッケージ・リポジトリにインストールされます。これらのパッケージは、どのユーザでもインポートできます。

Anaconda のような代替ディストリビューションに切り替える場合、InterSystems IRIS はそのパッケージ・リポジトリの場所を認識できない可能性があります。InterSystems IRIS は、正しいディレクトリ、すなわちディレクトリ <installdir>/lib/pythoniris_site.py ファイルを含むように sys.path を調整するのに役立つツールを提供します。

Ubuntu 22.04 で Anaconda を使用している場合は、iris_site.py ファイルを以下のように編集します。

import sys
from site import getsitepackages as __sitegetsitepackages
 
# modify EmbeddedPython to get site-packages from Anaconda python distribution.
 
def set_site_path(platform_name):
    sys.path = sys.path + __sitegetsitepackages(['/opt/anaconda3/'])

上記の iris_site.py では、組み込み Python の sys.path は以下のようになります。

USER>do ##class(%SYS.Python).Shell()

Python 3.11.7 (main, Dec 15 2023, 18:24:52) [GCC 11.2.0] on linux
Type quit() or Ctrl-D to exit this shell.
>>> import sys
>>> sys.path
['/opt/anaconda3/lib/python311.zip', '/opt/anaconda3/lib/python3.11', '/opt/anaconda3/lib/python3.11/lib-dynload', 
'/InterSystems/IRIS/lib/python', '/InterSystems/IRIS/mgr/python', '/opt/anaconda3/lib/python3.11/site-packages']

フレキシブル Python ランタイムに適した sys.path にするのに、何回かの反復処理が必要になることがあります。InterSystems IRIS 外で Python を起動し、その sys.path を組み込み Python 内の sys.path と比較して、想定されるディレクトリがすべて含まれていることを確認するとよいでしょう。

Note:

PythonRuntimeLibrary 構成設定または iris_site.py に対する変更は、新しいセッションの開始時に有効になります。InterSystems IRIS を再起動する必要はありません。

Python バージョン情報の確認

フレキシブル Python ランタイム機能を使用している場合、組み込み Python が使用している Python のバージョンと、システムで使用されている既定のバージョンを確認すると便利なことがあります。そのためには、%SYS.PythonOpens in a new tab クラスの GetPythonInfo() メソッドを呼び出すと簡単です。

以下の例は、x86 アーキテクチャの Ubuntu 22.04 で、使用されている Python ランタイム・ライブラリが /usr/lib/x86_64-linux-gnu/libpython3.10.so.1、実行されている組み込み Python のバージョンが 3.10.13、システム・バージョンが 3.8.10 であることを示しています。

USER>do ##class(%SYS.Python).GetPythonInfo(.info)

USER>zw info
info("CPF_PythonPath")=""
info("CPF_PythonRuntimeLibrary")="/usr/lib/x86_64-linux-gnu/libpython3.10.so.1"
info("RunningLibrary")="/usr/lib/x86_64-linux-gnu/libpython3.10.so.1"
info("RunningVersion")="3.10.13 (main, Aug 25 2023, 13:20:03) [GCC 9.4.0]"
info("SystemPath")="/usr/lib/python3.8/config-3.8-x86_64-linux-gnu"
info("SystemVersion")="3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]"
info("SystemVersionShort")="3.8.10"

この情報は、オペレーティング・システム、Python バージョン、およびその他の要素によって異なります。

フレキシブル Python ランタイムの例 : Ubuntu 22.04 の Python 3.11

Python 3.10 は、Ubuntu 22.04 の Python の既定バージョンです。この例では、組み込み Python で Python 3.11 を使用する方法を示します。

Note:

この例は、x86 アーキテクチャの Ubuntu 22.04 の場合です。ARM アーキテクチャの場合、ファイル名とディレクトリ名は異なることがあります。

  1. コマンド行から Python 3.11 をインストールします。

    $ sudo apt install python3.11-full

  2. Python 3.11 libpython.so 共有ライブラリをインストールします。

    $ sudo apt install libpython3.11

  3. PythonRuntimeLibrary 構成設定を /usr/lib/x86_64-linux-gnu/libpython3.11.so.1 に設定します。

    詳細は、"PythonRuntimeLibrary" を参照してください。

  4. ターミナルから組み込み Python を起動し、sys.path に Python 3.11 パッケージ・ディレクトリが含まれていることを確認します。

    USER>do ##class(%SYS.Python).Shell()
    
    Python 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0] on linux
    Type quit() or Ctrl-D to exit this shell.
    >>> import sys
    >>> sys.path
    ['/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/InterSystems/IRIS/lib/python', 
    '/InterSystems/IRIS/mgr/python', '/usr/local/lib/python3.11/dist-packages', '/usr/lib/python3/dist-packages', 
    '/usr/lib/python3.11/dist-packages']
    >>> 
    
    
  5. ターミナルから %SYS.PythonOpens in a new tab クラスの GetPythonInfo() メソッドを使用して、Python のバージョン情報を表示します。

    USER>do ##class(%SYS.Python).GetPythonInfo(.info)
    
    USER>zw info
    info("AllowNonSystemPythonForIntegratedML")=0
    info("CPF_PythonPath")=""
    info("CPF_PythonRuntimeLibrary")="/usr/lib/x86_64-linux-gnu/libpython3.11.so.1"
    info("RunningLibrary")="/usr/lib/x86_64-linux-gnu/libpython3.11.so.1"
    info("RunningVersion")="3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0]"
    info("SystemPath")="/usr/lib/python3.10/config-3.10-x86_64-linux-gnu"
    info("SystemVersion")="3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]"
    info("SystemVersionShort")="3.10.6"
    

    この例は、使用されている Python ランタイム・ライブラリが /usr/lib/x86_64-linux-gnu/libpython3.11.so.1、実行されている組み込み Python のバージョンが 3.11.0rc1、システム・バージョンが 3.10.6 であることを示しています。

フレキシブル Python ランタイムの例 : Ubuntu 22.04 の Anaconda

Anaconda は、一般的にデータ・サイエンスや人工知能アプリケーションに使用される Python ベースのプラットフォームです。この例では、Ubuntu 22.04 で Anaconda を使用する方法を示します。

Note:

この例は、x86 アーキテクチャの Ubuntu 22.04 の場合です。ARM アーキテクチャの場合、ファイル名とディレクトリ名は異なることがあります。

  1. https://www.anaconda.com/download から Anaconda をダウンロードします。

    ダウンロードは、Anaconda3-2024.02-1-Linux-x86_64.sh. のような名前のシェル・スクリプトです。

  2. コマンド行から、以下のように Anaconda インストール・スクリプトを実行します。

    $ sudo sh Anaconda3-2024.02-1-Linux-x86_64.sh -b -p /opt/anaconda3

  3. PythonRuntimeLibrary 構成設定を /opt/anaconda3/lib/libpython3.11.so に設定します。

    詳細は、"PythonRuntimeLibrary" を参照してください。

  4. ディレクトリ <installdir>/lib/pythoniris_site.py ファイルを編集して、Anaconda パッケージ・リポジトリを sys.path に追加します。

    import sys
    from site import getsitepackages as __sitegetsitepackages
     
    # modify EmbeddedPython to get site-packages from Anaconda python distribution.
     
    def set_site_path(platform_name):
        sys.path = sys.path + __sitegetsitepackages(['/opt/anaconda3/'])
    
  5. 組み込み Python を起動してエラー <class 'ModuleNotFoundError'>: No module named 'math' - iris loader failed が表示された場合は、PATH 環境変数を編集してディレクトリ /opt/anaconda3/bin を先頭に追加します。

    $ export PATH=/opt/anaconda3/bin:$PATH
    $ env |grep PATH
    PATH=/opt/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
    
  6. ターミナルから組み込み Python を起動し、sys.path に Anaconda パッケージ・リポジトリが含まれていることを確認します。

    USER>do ##class(%SYS.Python).Shell()
    
    Python 3.11.7 (main, Dec 15 2023, 18:24:52) [GCC 11.2.0] on linux
    Type quit() or Ctrl-D to exit this shell.
    >>> import sys
    >>> sys.path
    ['/opt/anaconda3/lib/python311.zip', '/opt/anaconda3/lib/python3.11', '/opt/anaconda3/lib/python3.11/lib-dynload', 
    '/InterSystems/IRIS/lib/python', '/InterSystems/IRIS/mgr/python', '/opt/anaconda3/lib/python3.11/site-packages']
    
    
  7. ターミナルから %SYS.PythonOpens in a new tab クラスの GetPythonInfo() メソッドを使用して、Python のバージョン情報を表示します。

    USER>zw info
    info("AllowNonSystemPythonForIntegratedML")=0
    info("CPF_PythonPath")=""
    info("CPF_PythonRuntimeLibrary")="/opt/anaconda3/lib/libpython3.11.so"
    info("RunningLibrary")="/opt/anaconda3/lib/libpython3.11.so"
    info("RunningVersion")="3.11.7 (main, Dec 15 2023, 18:24:52) [GCC 11.2.0]"
    info("SystemPath")="/usr/lib/python3.10/config-3.10-x86_64-linux-gnu"
    info("SystemVersion")="3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]"
    info("SystemVersionShort")="3.10.6"
    

    この例は、使用されている Python ランタイム・ライブラリが /opt/anaconda3/lib/libpython3.11.so、実行されている組み込み Python のバージョンが 3.11.7、システム・バージョンが 3.10.6 であることを示しています。

FeedbackOpens in a new tab