Skip to content

Commit

Permalink
remove force imports
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Oct 16, 2023
1 parent 459b3a8 commit a8713f6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
6 changes: 0 additions & 6 deletions .conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ build:


requirements:
build:
- "python>=3.8"
- setuptools
- "ase>=3.22"
- pip
host:
- "python>=3.8"
- "ase>=3.22"
- pip
run:
- "python>=3.8"
Expand Down
38 changes: 34 additions & 4 deletions sparc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
from .io import read_sparc, write_sparc
from .io import register_ase_io_sparc
from .calculator import SPARC
"""Initialization of sparc-x-api
register_ase_io_sparc()
For submodules like download_data and api, ase / numpy may be ignored,
and run using standard python libaries. This may be useful for cases like
conda build and CI where not all dependencies are present
"""

def _missing_deps_func(*args, **kwargs):
raise ImportError("Importing fails for ase / numpy!")

class SPARCMissingDeps:
def __init__(self, *args, **kwargs):
raise ImportError("Cannot initialize sparc.SPARC because the required dependencies (ase and numpy) are not available.")

def __getattr__(self, name):
raise ImportError(f"Cannot access '{name}' on sparc.SPARC because the required dependencies (ase and numpy) are not available.")
try:
import ase
import numpy
_import_complete = True
except ImportError:
_import_complete = False

if _import_complete:
from .io import read_sparc, write_sparc
from .io import register_ase_io_sparc
from .calculator import SPARC
register_ase_io_sparc()
else:
# If importing is not complete, any code trying to directly import
# the following attributes will raise ImportError
read_sparc = _missing_deps_func
write_sparc = _missing_deps_func
SPARC = SPARCMissingDeps

0 comments on commit a8713f6

Please sign in to comment.