Skip to content

Commit e17e2d0

Browse files
committed
try/except imports below regular imports
1 parent 3161068 commit e17e2d0

File tree

7 files changed

+27
-38
lines changed

7 files changed

+27
-38
lines changed

pymatgen/io/abinit/netcdf.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
from pymatgen.core.units import ArrayWithUnit
1919
from pymatgen.core.xcfunc import XcFunc
2020

21+
try:
22+
import netCDF4
23+
except ImportError:
24+
netCDF4 = None
25+
warnings.warn("Can't import netCDF4. Some features will be disabled unless you pip install netCDF4.")
26+
27+
2128
logger = logging.getLogger(__name__)
2229

2330
__author__ = "Matteo Giantomassi"
@@ -37,21 +44,6 @@
3744
"structure_from_ncdata",
3845
]
3946

40-
try:
41-
import netCDF4
42-
except ImportError as exc:
43-
netCDF4 = None
44-
warnings.warn(
45-
f"""\
46-
`import netCDF4` failed with the following error:
47-
48-
{exc}
49-
50-
Please install netcdf4 with `conda install netcdf4`
51-
If the conda version does not work, uninstall it with `conda uninstall hdf4 hdf5 netcdf4`
52-
and use `pip install netcdf4`"""
53-
)
54-
5547

5648
def _asreader(file, cls):
5749
closeit = False

pymatgen/phonon/thermal_displacements.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
from pymatgen.symmetry.groups import SYMM_DATA
1515
from pymatgen.util.due import Doi, due
1616

17-
sub_spgrp = partial(re.sub, r"[\s_]", "")
18-
19-
space_groups = {sub_spgrp(k): k for k in SYMM_DATA["space_group_encoding"]} # type: ignore
20-
2117
try:
2218
import phonopy
2319
except ImportError as exc:
@@ -32,6 +28,10 @@
3228
__status__ = "Testing"
3329
__date__ = "August 09, 2022"
3430

31+
sub_spgrp = partial(re.sub, r"[\s_]", "")
32+
33+
space_groups = {sub_spgrp(k): k for k in SYMM_DATA["space_group_encoding"]} # type: ignore
34+
3535

3636
class ThermalDisplacementMatrices(MSONable):
3737
"""Class to handle thermal displacement matrices

pymatgen/vis/structure_vtk.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
from typing import Sequence
1111

1212
import numpy as np
13+
from monty.dev import requires
14+
from monty.serialization import loadfn
15+
16+
from pymatgen.core.periodic_table import Species
17+
from pymatgen.core.sites import PeriodicSite
18+
from pymatgen.core.structure import Structure
19+
from pymatgen.util.coord import in_coord_list
1320

1421
try:
1522
import vtk
@@ -19,14 +26,6 @@
1926
vtk = None
2027
vtkInteractorStyleTrackballCamera = object
2128

22-
from monty.dev import requires
23-
from monty.serialization import loadfn
24-
25-
from pymatgen.core.periodic_table import Species
26-
from pymatgen.core.sites import PeriodicSite
27-
from pymatgen.core.structure import Structure
28-
from pymatgen.util.coord import in_coord_list
29-
3029
module_dir = os.path.dirname(os.path.abspath(__file__))
3130
EL_COLORS = loadfn(f"{module_dir}/ElementColorSchemes.yaml")
3231

tests/analysis/test_graphs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from openbabel import openbabel
3131
except ImportError:
3232
openbabel = None
33+
3334
try:
3435
import pygraphviz
3536
except ImportError:

tests/io/test_shengbte.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
from pymatgen.io.shengbte import Control
99
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
1010

11-
test_dir = f"{TEST_FILES_DIR}/shengbte"
12-
13-
this_dir = os.path.dirname(os.path.abspath(__file__))
14-
1511
try:
1612
import f90nml
1713
except ImportError:
1814
f90nml = None
1915

16+
test_dir = f"{TEST_FILES_DIR}/shengbte"
17+
18+
this_dir = os.path.dirname(os.path.abspath(__file__))
19+
2020

2121
class TestShengBTE(PymatgenTest):
2222
def setUp(self):

tests/phonon/test_gruneisen.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import matplotlib.pyplot as plt
66
from pytest import approx
77

8-
from pymatgen.util.testing import TEST_FILES_DIR
8+
from pymatgen.io.phonopy import get_gruneisen_ph_bs_symm_line, get_gruneisenparameter
9+
from pymatgen.phonon.gruneisen import GruneisenParameter
10+
from pymatgen.phonon.plotter import GruneisenPhononBandStructureSymmLine, GruneisenPhononBSPlotter, GruneisenPlotter
11+
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
912

1013
try:
1114
import phonopy
@@ -14,11 +17,6 @@
1417
print(exc)
1518
phonopy = TotalDos = None
1619

17-
from pymatgen.io.phonopy import get_gruneisen_ph_bs_symm_line, get_gruneisenparameter
18-
from pymatgen.phonon.gruneisen import GruneisenParameter
19-
from pymatgen.phonon.plotter import GruneisenPhononBandStructureSymmLine, GruneisenPhononBSPlotter, GruneisenPlotter
20-
from pymatgen.util.testing import PymatgenTest
21-
2220

2321
class TestGruneisenPhononBandStructureSymmLine(PymatgenTest):
2422
def setUp(self) -> None:

tests/transformations/test_advanced_transformations.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
except ImportError:
5252
hiphive = None
5353

54-
5554
try:
5655
import matgl
5756
except ImportError:

0 commit comments

Comments
 (0)