Skip to content

Commit 62df744

Browse files
Merge pull request #608 from mattwthompson/import-pybel-2-3
Import pybel in a openbabel 2/3 compatible manner
2 parents e8a8314 + c082cef commit 62df744

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

mbuild/compound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def load(filename_or_object, relative_to_module=None, compound=None, coords_only
8383
md.Trajectory:compound.from_trajectory,
8484
}
8585
try:
86-
import pybel
86+
pybel = import_('pybel')
8787
type_dict.update({pybel.Molecule:compound.from_pybel})
8888
except ImportError:
8989
pass

mbuild/tests/test_compound.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import mbuild as mb
99
from mbuild.exceptions import MBuildError
1010
from mbuild.utils.geometry import calc_dihedral
11-
from mbuild.utils.io import get_fn, has_foyer, has_intermol, has_openbabel, has_networkx
11+
from mbuild.utils.io import get_fn, import_, has_foyer, has_intermol, has_openbabel, has_networkx
1212
from mbuild.tests.base_test import BaseTest
1313

1414
class TestCompound(BaseTest):
@@ -948,7 +948,7 @@ def test_to_pybel(self, ethane):
948948

949949
@pytest.mark.skipif(not has_openbabel, reason="Pybel is not installed")
950950
def test_from_pybel(self):
951-
import pybel
951+
pybel = import_('pybel')
952952
benzene = list(pybel.readfile('mol2', get_fn('benzene.mol2')))[0]
953953
cmpd = mb.Compound()
954954
cmpd.from_pybel(benzene)
@@ -972,15 +972,15 @@ def test_to_more_pybel_residues(self, methane, ethane):
972972

973973
@pytest.mark.skipif(not has_openbabel, reason="Pybel is not installed")
974974
def test_from_pybel_residues(self):
975-
import pybel
975+
pybel = import_('pybel')
976976
pybel_mol = list(pybel.readfile('mol2', get_fn('methyl.mol2')))[0]
977977
cmpd = mb.Compound()
978978
cmpd.from_pybel(pybel_mol)
979979
assert 'LIG1' in cmpd.children[0].name
980980

981981
@pytest.mark.skipif(not has_openbabel, reason="Pybel is not installed")
982982
def test_from_pybel_monolayer(self):
983-
import pybel
983+
pybel = import_('pybel')
984984
monolayer = list(pybel.readfile('pdb', get_fn('monolayer.pdb')))[0]
985985
# TODO: Actually store the box information
986986
cmpd = mb.Compound()

mbuild/utils/io.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pkg_resources import resource_filename
2424
import sys
2525
import textwrap
26+
import warnings
2627
from unittest import SkipTest
2728

2829

@@ -44,7 +45,7 @@ class DelayImportError(ImportError, SkipTest):
4445
4546
nglview can be installed using:
4647
47-
# conda install -c bioconda nglview
48+
# conda install -c conda-forge nglview
4849
4950
or
5051
@@ -56,7 +57,7 @@ class DelayImportError(ImportError, SkipTest):
5657
5758
openbabel can be installed with conda using:
5859
59-
# conda install -c bioconda openbabel
60+
# conda install -c conda-forge openbabel
6061
6162
or from source following instructions at:
6263
@@ -99,7 +100,27 @@ def import_(module):
99100
>>> # module from, etc) if the import fails
100101
>>> import tables
101102
>>> tables = import_('tables')
103+
104+
Notes
105+
-----
106+
The pybel/openbabel block is meant to resolve compatibility between
107+
openbabel 2.x and 3.0. There may be other breaking changes but the change
108+
in importing them is the major one we are aware of. For details, see
109+
https://open-babel.readthedocs.io/en/latest/UseTheLibrary/migration.html#python-module
102110
"""
111+
if module == 'pybel':
112+
try:
113+
return importlib.import_module('openbabel.pybel')
114+
except ModuleNotFoundError:
115+
pass
116+
try:
117+
pybel = importlib.import_module('pybel')
118+
msg = ('openbabel 2.0 detected and will be dropped in a future '
119+
'release. Consider upgrading to 3.x.')
120+
warnings.warn(msg, DeprecationWarning)
121+
return pybel
122+
except ModuleNotFoundError:
123+
pass
103124
try:
104125
return importlib.import_module(module)
105126
except ImportError as e:

0 commit comments

Comments
 (0)