Embedded Python - 安装及初步使用
在Windows中,InterSystems IRIS 会将Python引擎一起安装在安装目录中,可以将Python的代码在InterSystems IRIS内核中运行,允许Python代码与ObjectScript代码混合运行,以获得最佳开发性能。一般基于UNIX的操作系统会自带一个Python,所以不会随InterSystems IRIS安装包自动安装Python引擎。
在InterSystems IRIS 2021.2 以上的版本中才支持Embedded Python,其余版本不支持使用Embedded Python
步骤 - Windows
- 在Microsoft Windows 上安装InterSystems IRIS 2022.2版本
- Python 引擎同InterSystems IRIS 安装包安装在C:\InterSystems\IRISHealth\lib\python 文件夹下(如果使用默认安装路径)。
- 在C:\InterSystems\IRISHealth\lib\python 文件夹下,查看Python的版本,版本应为Python 3.9.5
C:\InterSystems\IRISHealth\lib\python>python --version
- 使用pip下载pandas库,其中InterSystems\IRIS\mgr\python路径根据安装路径进行更改。(其他python库,也按照此方法下载)
C:\InterSystems\IRISHealth\bin>irispip install --target C:\InterSystems\IRIS\mgr\python pandas
- 将Data.Titanic数据导入InterSystems IRIS,或者创建一个其它的表,然后插入任何数据。
- 使用InterSystems IRIS Studio创建一个Sample.EmbeddedPython类,其中Data.Titanic 可以改为任意你自己创建好的表。[ Language = python ]表示此方法为一个Python方法,里面使用的是Python开发语言。
/// Embedded Python examples from summer 2022 Class Sample.EmbeddedPython Extends %RegisteredObject { ClassMethod dfexample() As %Status { set st = ..CreateDataFrame("Name, Super, TimeCreated") } // Execute a SQL query from Python and import it into a pandas dataframe ClassMethod CreateDataFrame(myfields As %String) As %Numeric [ Language = python ] { import iris import pandas as pd #; works with all IRIS installs #; rs = iris.sql.exec("SELECT " + myfields + " FROM %Dictionary.ClassDefinition WHERE Name %STARTSWITH '%Net.'") #; works with Titanic sample data rs = iris.sql.exec("SELECT * FROM Data.Titanic") df = rs.dataframe() print(df.describe()) return True } } - 在InterSystems IRIS Terminal执行,即可通过Python查看Data.Titanic 表中所有的数据。
do ##class(Sample.EmbeddedPython).dfexample()
步骤 - UNIX
- 在UNIX操作系统上安装InterSystems IRIS 2022.2版本
- 一般基于UNIX的操作系统会自带一个Python, 也可以按照下列方式重新安装Python。
macOS: Install Python 3.9 using Homebrew (https://formulae.brew.sh/formula/python@3.9Opens in a new tab) Ubuntu: apt-get install python3 Red Hat Enterprise Linux or Oracle Linux: yum install python3 SUSE: zypper install python3
- 一般基于UNIX的操作系统会自带一个Python, 也可以按照下列方式重新安装Python。
- 使用pip下载pandas库,其中InterSystems\IRIS\mgr\python路径根据安装路径进行更改。(其他python库,也按照此方法下载)
$ pip3 install --target /InterSystems/IRIS/mgr/python numpy
- 将Data.Titanic数据导入InterSystems IRIS,或者创建一个其它的表,然后插入任何数据。
- 使用InterSystems IRIS Studio创建一个Sample.EmbeddedPython类,其中Data.Titanic 可以改为任意你自己创建好的表。[ Language = python ]表示此方法为一个Python方法,里面使用的是Python开发语言。
/// Embedded Python examples from summer 2022 Class Sample.EmbeddedPython Extends %RegisteredObject { ClassMethod dfexample() As %Status { set st = ..CreateDataFrame("Name, Super, TimeCreated") } // Execute a SQL query from Python and import it into a pandas dataframe ClassMethod CreateDataFrame(myfields As %String) As %Numeric [ Language = python ] { import iris import pandas as pd #; works with all IRIS installs #; rs = iris.sql.exec("SELECT " + myfields + " FROM %Dictionary.ClassDefinition WHERE Name %STARTSWITH '%Net.'") #; works with Titanic sample data rs = iris.sql.exec("SELECT * FROM Data.Titanic") df = rs.dataframe() print(df.describe()) return True } } - 在InterSystems IRIS Terminal执行,即可通过Python查看Data.Titanic 表中所有的数据。
do ##class(Sample.EmbeddedPython).dfexample()
注意事项
如果你得到一个 "Failed to load python " 或者python3 distinct from irispython not found: No such file or directory的错误,这意味着你要么没有安装Python,要么在你的系统上安装了一个其他的Python版本。请通过上述方式,安装Python。
为了防止运行嵌入式Python时出现IRIS_ACCESSDENIED错误,请启用%Service_Callin。在管理门户中,进入系统管理 > 安全 > 服务,选择 %Service_CallIn,并选中已启用的服务框。
在基于 UNIX 的系统上,你需要用 pip3 命令来安装 Python 包。如果你还没有安装 pip3,用你系统的软件包管理器安装 python3-pip 包。