When you run Embedded Python, InterSystems IRIS expects that you are using the default version of Python for your operating system. However, there are times when you might want to upgrade to a later version of Python or switch to an alternate distribution like Anaconda. The Flexible Python Runtime feature enables you to use these versions of Python with Embedded Python in InterSystems IRIS.
Embedded Python and sys.path
After you launch Embedded Python, it uses the directories contained in the sys.path variable to locate any Python packages you want to import.
When you use Embedded Python with the default version of Python for your operating system, sys.path already includes the correct directories, for example.
-
<installdir>/lib/python (Python packages reserved for InterSystems IRIS)
-
<installdir>/mgr/python (Python packages installed by the user)
-
Global package repositories for the default Python version
For example: /usr/local/lib/python3.10/dist-packages.
This location will vary based on your operating system, Python version, and other factors. The example shown here is for Python 3.10 on Ubuntu 22.04.
On Ubuntu 22.04, with the default version of Python (3.10), sys.path in Embedded Python might look something like this:
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:
If sys.path contains a directory in that is within your home directory, such as /home/<user>/.local/lib/python3.10/site-packages, it could indicate that you have installed packages in your local package repository. For example, if you install a package without the --target attribute (and without using sudo), Python will install it into the local package repository within your home directory. If any other user tries to import the package, it will fail.
Although InterSystems recommends using the --target <installdir>/mgr/python option, installing packages using sudo and omitting the --target attribute installs the packages into the global package repository. These packages can also be imported by any user.
If you switch to an alternate distribution like Anaconda, InterSystems IRIS may not know where its package repositories are located. InterSystems IRIS provides you with a tool that can help you tailor your sys.path to include the correct directories, namely the iris_site.py file in the directory <installdir>/lib/python.
For Ubuntu 22.04, when using Anaconda, edit your iris_site.py file to look something like the following:
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/'])
With the iris_site.py above, sys.path in Embedded Python might look something like this:
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']
It may take you a few iterations to get your sys.path correct for Flexible Python Runtime. It may be helpful to launch Python outside of InterSystems IRIS and compare its sys.path with the sys.path inside Embedded Python to make sure you have all of the expected directories.
Note:
Changes to the PythonRuntimeLibrary configuration setting or iris_site.py take effect on starting a new session. Restarting InterSystems IRIS is not required.
Check Python Version Information
If you are using the Flexible Python Runtime feature, it can be useful to check the version of Python that Embedded Python is using versus the default version that your system is using. An easy way to do this is by calling the GetPythonInfo() method of the %SYS.PythonOpens in a new tab class.
The following example, on Ubuntu 20.04 on the x86 architecture, shows that the Python runtime library being used is /usr/lib/x86_64-linux-gnu/libpython3.10.so.1, the running version of Embedded Python is 3.10.13, and the system version is 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"
This information will vary based on your operating system, Python version, and other factors.