Skip to main content

This documentation is for an older version of this product. See the latest version of this content.Opens in a new tab

Install and Import Python Packages

Embedded Python gives you easy access to thousands of useful libraries. Commonly called “packages,” these need to be installed into the InterSystems IRIS file system before they can be used. Then they need to imported to load them into memory for use by your code. There are different ways to do this, depending on how you will use Embedded Python.

Install Python Packages

Install Python packages from the command line before using them with Embedded Python. The command you use differs depending on whether you are using InterSystems IRIS on a UNIX-based system, on Windows, or in a container.

Install Python Packages on a UNIX-Based System

On UNIX-based systems, use the pip3 command: pip3 install --target <installdir>/mgr/python <package>.

For example, the ReportLab Toolkit is an open source library for generating PDFs and graphics. On a UNIX-based system, use a command like the following to install it:

$ pip3 install --target /InterSystems/IRIS/mgr/python reportlab
Note:

If you do not have pip3 installed already, install the package python3-pip with your system’s package manager.

Install Python Packages on Windows

On Windows, use the built-in irispip command from the <installdir>/bin directory: irispip install --target <installdir>\mgr\python <package>.

For example, you can install the ReportLab package on a Windows machine as follows:

C:\InterSystems\IRIS\bin>irispip install --target C:\InterSystems\IRIS\mgr\python reportlab

Install Python Packages in a Container

If you are running InterSystems IRIS in a container without using the durable %SYS feature, use the pip3 command: pip3 install --target /usr/irissys/mgr/python <package>.

For example, you can install the ReportLab package in the container as follows:

$ pip3 install --target /usr/irissys/mgr/python reportlab 

If you are running InterSystems IRIS in a container using the durable %SYS feature, use the pip3 command: pip3 install --target <durable>/mgr/python <package>, where <durable> is the path defined in the environment variable ISC_DATA_DIRECTORY when running the container.

For example, if ISC_DATA_DIRECTORY=/durable/iris, you can install the ReportLab package in the container as follows:

$ pip3 install --target /durable/iris/mgr/python reportlab 
Note:

Note: If you are using a Dockerfile to create a custom Docker image for InterSystems IRIS, install Python packages in /usr/irissys/mgr/python. If then you run the custom container using the durable %SYS feature, set the environment variable PYTHONPATH=/usr/irissys/mgr/python so that this directory is included in sys.path in addition to <durable>/mgr/python.

For more information on creating and running containers, see Running InterSystems Products in Containers.

Import Python Packages

After installing a package, you need to import it before you can use it from InterSystems IRIS. This loads the package into memory so that it is available for use.

Import Python Packages from ObjectScript

To import a Python package from ObjectScript, use the Import() method of the %SYS.PythonOpens in a new tab class. For example:

set pymath = ##class(%SYS.Python).Import("math")
set canvaslib = ##class(%SYS.Python).Import("reportlab.pdfgen.canvas")

The first line, above, imports the built-in Python math module into ObjectScript. The second line imports just the canvas.py file from the pdfgen subpackage of ReportLab.

Import Python Packages from a Method Written in Python

You can import packages in an InterSystems IRIS method written in Python, just as you would in any other Python code, for example:

ClassMethod Example() [ Language = python ] 
{
     import math
     import iris
     import reportlab.pdfgen.canvas as canvaslib
     
     # Your Python code here
} 

Import Python Packages via an XData Block

You can also import a list of packages using an XData block in a class, as in the following example:

XData %import [ MimeType = application/python ]
{
     import math 
     import iris
     import reportlab.pdfgen.canvas as canvaslib
} 
Important:

The name of the XData block must be %import. The MIME type can be application/python or text/x-python. Make sure to use correct Python syntax, including considerations of line indentation.

These packages can then be used in any method within the class that is written in Python, without needing to import them again.

ClassMethod Test() [ Language = python ] 
{
   # Packages imported in XData block

     print('\nValue of pi from the math module:')
     print(math.pi)
     print('\nList of classes in this namespace from the iris module:')
     iris.cls('%SYSTEM.OBJ').ShowClasses() 
} 

For background information on XData blocks, see Defining and Using XData Blocks

FeedbackOpens in a new tab