Skip to content

Cannot import Interchange if openmm is not installed #1527

Description

@stgeo

Description

I have a package that depends on openff-interchange but does not install it via conda, so I have to explicitly specify the dependencies from the git repositories:

[project.optional-dependencies]
openff = [
    "openff-toolkit @ git+https://github.com/openforcefield/openff-toolkit@stable",
    "openff-interchange @ git+https://github.com/openforcefield/openff-interchange@v0.3.29",
    "openff-models @ git+https://github.com/openforcefield/openff-models@0.1.2",
    "openff-recharge @ git+https://github.com/openforcefield/openff-recharge@0.5.2",
    "openff-units @ git+https://github.com/openforcefield/openff-units@0.2.2",
    "openff-utilities @ git+https://github.com/openforcefield/openff-utilities@v0.1.12",
    "openforcefields @ git+https://github.com/openforcefield/openforcefields@2024.09.0",
    "networkx",
    "pint",
    "importlib-resources",
    "importlib-metadata",
    "cachetools",
    "xmltodict",
    "python-constraint",
]

which works fine. However, bumping the versions to:

[project.optional-dependencies]
openff = [
    "openff-toolkit @ git+https://github.com/openforcefield/openff-toolkit@stable",
    "openff-interchange @ git+https://github.com/openforcefield/openff-interchange@v0.5.4",
    "openff-models @ git+https://github.com/openforcefield/openff-models@0.1.2",
    "openff-recharge @ git+https://github.com/openforcefield/openff-recharge@0.5.3",
    "openff-units @ git+https://github.com/openforcefield/openff-units@0.4.0",
    "openff-utilities @ git+https://github.com/openforcefield/openff-utilities@v0.1.17",
    "openforcefields @ git+https://github.com/openforcefield/openforcefields@2025.10.0",
    ...
]

Importing Interchange as

from openff.interchange.components.interchange import Interchange

I now get an exception:

NameError: name 'openmm' is not defined
Traceback (most recent call last):
  ...
  File "/redacted/.venv/lib/python3.13/site-packages/openff/interchange/components/interchange.py", line 28, in <module>
    from openff.interchange.components.mdconfig import MDConfig
  File "/redacted/.venv/lib/python3.13/site-packages/openff/interchange/components/mdconfig.py", line 12, in <module>
    from openff.interchange.components.toolkit import _get_num_h_bonds
  File "/redacted/.venv/lib/python3.13/site-packages/openff/interchange/components/toolkit.py", line 125, in <module>
    system: openmm.System | None = None,
            ^^^^^^
NameError: name 'openmm' is not defined. Did you mean: 'open'?

The offending code is:

def _simple_topology_from_openmm(
    openmm_topology: "openmm.app.Topology",
    system: openmm.System | None = None,
) -> Topology:

openmm.app is conditionally imported, if installed, and openmm is not imported at all.

Enclosing openmm.System | None in quotes resolves the issue, although it should probably also be imported under if TYPE_CHECKING.

Reproduction

mkdir test && cd test
uv init --python 3.13
uv add "openff-toolkit @ git+https://github.com/openforcefield/openff-toolkit@stable" \
	"openff-interchange @ git+https://github.com/openforcefield/openff-interchange@v0.5.4" \
	"openff-models @ git+https://github.com/openforcefield/openff-models@0.1.2" \
	"openff-recharge @ git+https://github.com/openforcefield/openff-recharge@0.5.3" \
	"openff-units @ git+https://github.com/openforcefield/openff-units@0.4.0" \
	"openff-utilities @ git+https://github.com/openforcefield/openff-utilities@v0.1.17" \
	"openforcefields @ git+https://github.com/openforcefield/openforcefields@2025.10.0" \
	"networkx" \
	"pint" \
	"importlib-resources" \
	"importlib-metadata" \
	"cachetools" \
	"xmltodict" \
	"python-constraint" \
	"packaging"
uv run python -c "from openff.interchange.components.interchange import Interchange"

Output

WARNING: No basic cheminformatics toolkits are available.
At least one basic toolkit is required to handle SMARTS matching and file I/O. 
Please install at least one of the following basic toolkits:
The RDKit : A conda-installable version of the free and open source RDKit cheminformatics toolkit can be found at: https://anaconda.org/conda-forge/rdkit
AmberTools : The AmberTools toolkit (free and open source) can be found at https://anaconda.org/conda-forge/ambertools
Built-in Toolkit : This toolkit is installed with the Open Force Field Toolkit and does not require additional dependencies.
OpenEye Toolkit : The OpenEye Toolkits can be installed via `mamba install openeye-toolkits -c openeye`
OpenFF NAGL : See https://docs.openforcefield.org/projects/nagl/en/latest/installation.html

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from openff.interchange.components.interchange import Interchange
  File "/redacted/test/.venv/lib/python3.13/site-packages/openff/interchange/components/interchange.py", line 28, in <module>
    from openff.interchange.components.mdconfig import MDConfig
  File "/redacted/test/.venv/lib/python3.13/site-packages/openff/interchange/components/mdconfig.py", line 12, in <module>
    from openff.interchange.components.toolkit import _get_num_h_bonds
  File "/redacted/test/.venv/lib/python3.13/site-packages/openff/interchange/components/toolkit.py", line 125, in <module>
    system: openmm.System | None = None,
            ^^^^^^
NameError: name 'openmm' is not defined. Did you mean: 'open'?

Software versions

  • Ubuntu 24.04, see above for the rest.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions