Activate virtual environment for the entire IRIS execution
Hello everyone.
After trying to run Python methods in the intersystemsdc/irishealth-community Docker container, which no longer allows any Python methods to be executed, I decided to abandon this version and start working with containers.intersystems.com/intersystems/iris-community.
In this environment, the Python methods work, which was a significant improvement.
I'm trying to import my libraries with pip install -r requirements.txt, but, as a very good security measure, it prevents this change from being applied to the general library, requiring that the installation be done in a virtual environment.
I have the following code in my Dockerfile.
ARG IMAGE=containers.intersystems.com/intersystems/iris-community:latest-em
FROM $IMAGE
USER root
WORKDIR /opt/irisbuild
RUN chown ${ISC_PACKAGE_MGRUSER}:${ISC_PACKAGE_IRISGROUP} /opt/irisbuild
USER ${ISC_PACKAGE_MGRUSER}
COPY src src
COPY data data
COPY iris.script iris.script
COPY requirements.txt requirements.txt
RUN python3 -m venv ${ISC_PACKAGE_INSTALLDIR}/.venv
RUN bash -c "source \"${ISC_PACKAGE_INSTALLDIR}/.venv/bin/activate\" && pip install -U -r requirements.txt"
RUN export IRIS_PYTHON_PATH=${ISC_PACKAGE_INSTALLDIR}/.venv/bin/
RUN iris start IRIS \
&& iris session IRIS < iris.script \
&& iris stop IRIS quietly
If I access the virtual environment via the terminal, I see that the packages have, indeed, been installed, but if I want to use them in an IRIS class, I can't access them because the application hasn't started the virtual environment.
I've consulted several forums, but they haven't clearly explained how to configure my IRIS instance to access the packages installed in the virtual environment.
In the docker-compose.yml file, I have this value in the environment section.
environment:
- IRIS_PYTHON_PATH=/usr/irissys/.venv/bin/
But it doesn't work. When I try to import any package installed in this environment, it can't find it.
Any suggestions?
Thanks in advance.
Comments
IRIS does not automatically “activate” a Python virtual environment for Embedded Python. The material found describes two supported patterns instead:
- install packages into the IRIS
mgr/pythonfolder withpip --target, or - use a virtual environment and explicitly add that venv’s
site-packagesdirectory tosys.pathbefore importing modules. [1]
For a virtual environment, the documented example is to append the venv site-packages path in ObjectScript, then import the package, for example: [1]
set sys = ##class(%SYS.Python).Import("sys")
do sys.path.append("/where/you/want/your/venv/lib/pythonX.X/site-packages")
set requests = ##class(%SYS.Python).Import("requests")
So, setting IRIS_PYTHON_PATH=/usr/irissys/.venv/bin/ in docker-compose.yml is not described in the retrieved material as the way to make Embedded Python find packages from a virtual environment. The retrieved material specifically points to adding the site-packages directory to sys.path, not the venv bin directory. [1]
If you want the packages to be available without managing a venv path, the retrieved material suggests installing them directly into the IRIS Python module location with pip --target=/path/to/your/iris/mgr/python .... [1]
The dev template material also states that it is “embedded python compatible” and that “all modules in requirements.txt will be installed during docker build,” but it does not provide additional details here about using IRIS_PYTHON_PATH to activate a venv for all IRIS execution. [2]
Sources:
Have a look to this article for details : https://community.intersystems.com/post/introduction-virtual-environments-python
and this lib to help you configure a single venv for iris easily : https://community.intersystems.com/post/unifying-embedded-python-and-native-api-iris-embedded-python-wrapper
python3 -m venv .venv
. .venv/bin/activate
pip install iris-embedded-python-wrapper
bind_iris