skills/omero-integration/references/connection.md
This reference is current for the skill snapshot dated 2026-07-23. It uses
omero-py==5.22.1 and the OMERO.server 5.6.18 documentation.
OMERO.server and its Python, web, Java, Bio-Formats, and Ice components have independent release numbers. Do not compare their version strings as if they were one package.
For the current stable pairing:
omero-py==5.22.1 declares Python >=3.10.For a different server release, read that release's history entry and use its tested OMERO.py version. A newest-client/old-server pairing may appear to work but is not the documented compatibility guarantee.
Use an isolated Python 3.12 environment and a platform-matched Ice wheel:
uv venv --python 3.12 .venv
source .venv/bin/activate
# Obtain the matching wheel from the OMERO-linked Ice binary matrix.
uv pip install "/absolute/path/to/zeroc_ice-3.6.5-<matching-tags>.whl"
uv pip install "omero-py==5.22.1"
Wheel tags must match all of:
cp310, cp311, or cp312)Do not silently fall back to compiling IcePy if the wheel is rejected. Inspect the interpreter and platform first. Do not install Ice 3.7 as a substitute.
OMERODIR is required for some CLI configuration and must point to a
compatible extracted OMERO.server tree to enable import and admin commands.
It is not required merely to use BlitzGateway against a remote server.
The bundled helpers read exactly these variables:
OMERO_HOST: required hostname, without http://, https://, or pathOMERO_PORT: optional integer, default 4064OMERO_USER: username for password authenticationOMERO_PASSWORD: password for password authenticationOMERO_SESSION_KEY: existing session key, alternative to user/passwordOMERO_SECURE: boolean, default trueRules:
.env files or read unrelated environment variables.Use try/finally when connection success must be checked explicitly:
import os
from omero.gateway import BlitzGateway
conn = BlitzGateway(
os.environ["OMERO_USER"],
os.environ["OMERO_PASSWORD"],
host=os.environ["OMERO_HOST"],
port=int(os.environ.get("OMERO_PORT", "4064")),
secure=True,
)
try:
if not conn.connect():
raise RuntimeError("OMERO connection failed")
# Keep reads bounded and group-scoped.
for image in conn.getObjects(
"Image",
opts={"limit": 25, "offset": 0, "order_by": "obj.id"},
):
print(image.getId())
finally:
conn.close()
BlitzGateway can also be a context manager. Its context manager calls
connect() and closes the underlying client:
import os
from omero.gateway import BlitzGateway
with BlitzGateway(
os.environ["OMERO_USER"],
os.environ["OMERO_PASSWORD"],
host=os.environ["OMERO_HOST"],
port=int(os.environ.get("OMERO_PORT", "4064")),
secure=True,
) as conn:
for project in conn.getObjects(
"Project",
opts={"limit": 10, "offset": 0, "order_by": "obj.id"},
):
print(project.getId())
Do not catch an exception merely to print its full representation: connection errors may include endpoint or identity details. Report the exception class and a scrubbed message; never include credential values.
BlitzGateway.connect() accepts sUuid, the existing session UUID:
import os
from omero.gateway import BlitzGateway
conn = BlitzGateway(
host=os.environ["OMERO_HOST"],
port=int(os.environ.get("OMERO_PORT", "4064")),
secure=True,
)
try:
if not conn.connect(sUuid=os.environ["OMERO_SESSION_KEY"]):
raise RuntimeError("Could not join the OMERO session")
print(conn.getEventContext().groupId)
finally:
conn.close()
Joining a session does not make it safe to log the key. If a low-level
omero.client is supplied through BlitzGateway(client_obj=client), the
gateway does not necessarily own every other use of that client. Close it only
when ownership is clear; the official context-manager example is appropriate
when nothing else uses the client.
The CLI stores sessions locally. Let it prompt:
omero login -s "$OMERO_HOST" -p "$OMERO_PORT" -u "$OMERO_USER"
omero sessions list
omero sessions file
omero logout
Do not use -w or --password. Although the CLI supports
OMERO_PASSWORD, avoid putting the secret in a persistent shell profile.
The CLI also supports joining a session with -k, but entering a session key
on the command line exposes it in shell history and process listings. Prefer a
short-lived, protected workflow and never paste the key into logs.
By default, session files are under ~/omero/sessions. OMERO_USERDIR or
OMERO_SESSIONDIR can change the location. Protect any custom directory with
user-only permissions and remove stale sessions with omero logout.
The default connection group comes from the session event context:
ctx = conn.getEventContext()
print(ctx.groupId) # Avoid printing the session ID.
Set one explicit accessible group before scoped queries:
group_id = 42
conn.SERVICE_OPTS.setOmeroGroup(str(group_id))
-1 requests cross-group behavior. It is not a harmless convenience:
# Only after the user explicitly requests all accessible groups:
conn.SERVICE_OPTS.setOmeroGroup("-1")
Do not set -1 by default, and do not combine it with an unbounded query.
Record the original group if temporarily changing context and restore it
before subsequent writes.
The CLI can switch its current session group:
omero group list
omero sessions group 42
Confirm the target group before import, link creation, table writes, ownership changes, or script execution.
secure=True DoesOfficial OMERO security documentation distinguishes authentication from later traffic:
BlitzGateway(..., secure=True) requests encryption for all transfers.Therefore, default to secure=True and the administrator-provided SSL router
port. Do not infer security merely from the number 4064.
Encryption is not the same as server identity verification. OME explicitly states that standard OMERO clients do not automatically verify the host, so a man-in-the-middle attack remains possible without additional configuration.
The official developer guidance lists these Ice properties for certificate validation:
IceSSL.Ciphers=HIGH (or a supported explicit cipher family)IceSSL.VerifyPeer=1IceSSL.VerifyDepthMax=0IceSSL.UsePlatformCAs=1, or IceSSL.CAs=/path/to/cacert.pemIceSSL.CheckCertName=1 for exact hostname checkingIceSSL.TrustOnly=... for documented alternative name restrictionsIceSSL.Protocols=tls1_2 if required by server policyThese are site-specific low-level client settings. Do not invent them from a hostname or disable verification to make a connection succeed. Ask the OMERO administrator for the CA, expected certificate name, router port, and policy. The bundled helpers enforce encrypted transport by default but do not claim to configure hostname verification.
For OMERO.web, use an administrator-managed HTTPS deployment with a recognized certificate. Never send JSON API credentials over plain HTTP.
BlitzGateway reuses stateless get...Service() proxies. Stateful services such
as rendering engines, raw stores, thumbnail stores, tables, and other
create... services should be created, used, and closed in the shortest
practicable scope.
Gateway recovery may recreate its own services after a connection failure. Client-held stateful proxies can then be stale. Do not retain them across long idle periods or reconnects.
Generic pattern:
store = conn.createRawFileStore()
try:
store.setFileId(original_file_id)
# Perform one explicitly bounded read.
finally:
store.close()
Closing the gateway is still mandatory even if every stateful child was closed.
Without exposing credentials:
OMERO_HOST has no URL scheme/path and OMERO_PORT is in range.secure=True.