Coverage for sacred/sacred/optional.py: 75%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# coding=utf-8
4import importlib
5from sacred.utils import modules_exist
6from sacred.utils import get_package_version, parse_version
9def optional_import(*package_names):
10 try:
11 packages = [importlib.import_module(pn) for pn in package_names]
12 return True, packages[0]
13 except ImportError:
14 return False, None
17def get_tensorflow():
18 # Ensures backward and forward compatibility with TensorFlow 1 and 2.
19 if get_package_version("tensorflow") < parse_version("1.13.1"):
20 import warnings
22 warnings.warn(
23 "Use of TensorFlow 1.12 and older is deprecated. "
24 "Use Tensorflow 1.13 or newer instead.",
25 DeprecationWarning,
26 )
27 import tensorflow as tf
28 else:
29 import tensorflow.compat.v1 as tf
30 return tf
33# Get libc in a cross-platform way and use it to also flush the c stdio buffers
34# credit to J.F. Sebastians SO answer from here:
35# http://stackoverflow.com/a/22434262/1388435
36try:
37 import ctypes
38 from ctypes.util import find_library
39except ImportError:
40 libc = None
41else:
42 try:
43 libc = ctypes.cdll.msvcrt # Windows
44 except OSError:
45 libc = ctypes.cdll.LoadLibrary(find_library("c"))
48has_numpy, np = optional_import("numpy")
49has_yaml, yaml = optional_import("yaml")
50has_pandas, pandas = optional_import("pandas")
52has_sqlalchemy = modules_exist("sqlalchemy")
53has_mako = modules_exist("mako")
54has_tinydb = modules_exist("tinydb", "tinydb_serialization", "hashfs")
55has_tensorflow = modules_exist("tensorflow")