diff --git a/yt/loaders.py b/yt/loaders.py index c1e32f44dbe..68a29b50ed3 100644 --- a/yt/loaders.py +++ b/yt/loaders.py @@ -5,15 +5,11 @@ import atexit import os import sys -import tarfile import time import types import warnings -from importlib.metadata import entry_points -from multiprocessing import Pipe, Process -from multiprocessing.connection import Connection from pathlib import Path -from typing import Any, Dict, List, Optional, Tuple, Union, cast +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast from urllib.parse import urlsplit import numpy as np @@ -44,6 +40,8 @@ ) from yt.utilities.on_demand_imports import _pooch as pooch, _ratarmount as ratarmount +if TYPE_CHECKING: + from multiprocessing.connection import Connection # --- Loaders for known data formats --- @@ -90,6 +88,8 @@ def load( yt.utilities.exceptions.YTAmbiguousDataType If the data format matches more than one class of similar specialization levels. """ + from importlib.metadata import entry_points + fn = os.path.expanduser(fn) if any(wildcard in fn for wildcard in "[]?!*"): @@ -1503,6 +1503,7 @@ def load_sample( - Corresponding sample data live at https://yt-project.org/data """ + import tarfile if fn is None: print( @@ -1636,7 +1637,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): def _mount_helper( - archive: str, mountPoint: str, ratarmount_kwa: Dict, conn: Connection + archive: str, mountPoint: str, ratarmount_kwa: Dict, conn: "Connection" ): try: fuseOperationsObject = ratarmount.TarMount( @@ -1697,6 +1698,8 @@ def load_archive( - This function requires ratarmount to be installed. - This function does not work on Windows system. """ + import tarfile + from multiprocessing import Pipe, Process warnings.warn( "The 'load_archive' function is still experimental and may be unstable.", diff --git a/yt/utilities/configure.py b/yt/utilities/configure.py index 64a034b7ee7..542dbb4e8b9 100644 --- a/yt/utilities/configure.py +++ b/yt/utilities/configure.py @@ -4,16 +4,10 @@ from pathlib import Path from typing import Callable, List -import tomli_w from more_itertools import always_iterable from yt.utilities.configuration_tree import ConfigLeaf, ConfigNode -if sys.version_info >= (3, 11): - import tomllib -else: - import tomli as tomllib - configuration_callbacks: List[Callable[["YTConfig"], None]] = [] @@ -89,6 +83,10 @@ def read(self, file_names): if not os.path.exists(fname): continue metadata = {"source": f"file: {fname}"} + if sys.version_info >= (3, 11): + import tomllib + else: + import tomli as tomllib try: with open(fname, "rb") as fh: data = tomllib.load(fh) @@ -104,6 +102,8 @@ def read(self, file_names): return file_names_read def write(self, file_handler): + import tomli_w + value = self.config_root.as_dict() config_as_str = tomli_w.dumps(value) diff --git a/yt/utilities/on_demand_imports.py b/yt/utilities/on_demand_imports.py index 2cc76904a46..07021988cb6 100644 --- a/yt/utilities/on_demand_imports.py +++ b/yt/utilities/on_demand_imports.py @@ -294,6 +294,8 @@ def __init__(self): # imported first, can get file lock errors on some systems (including travis-ci) # so we need to do this before initializing h5py_imports()! # similar to this issue https://github.com/pydata/xarray/issues/2560 + if find_spec("h5py") is None or find_spec("netCDF4") is None: + return try: import netCDF4 # noqa F401 except ImportError: