I'm trying to find the distances between residues of a protein in pymol using a python script, which calls the pymol command cmd.get_distance. However, sometimes there are multiple atom assignments, which causes an error:
GetDistance-Error: Selection 2 doesn't contain a single atom/vertex.
I want to skip over sites that have this problem so I'm trying to use try/pass:
try:
cmd.get_distance(atom2)
except GetDistance-Error:
pass
However, it tells me that there is no such error message:
NameError: global name 'GetDistance' is not defined.
How do I tell it to pass this error? isn't GetDistance-Error the error?
I stumbled over the same issue. I want to catch the error from pymols cmd.get_distance. In my case, one (or both) of the selections does not contain an atom at all (instead of 2).
This answer will not clarify handling error in pymol scripting but resolve the issue itself.
So instead of catching the error, you could check before if there is exactly 1 atom in your selection.
Erroneous code:
Optimized code:
So instead of catching the error, you check the requirements (one atom per selection) for the get_distance command. If you run a continuous code, the dist = "N/A" is needed, otherwise you will falsely get the last assignment of the distance.