Skip to content

Commit 771e2ed

Browse files
committed
Print error if required package is missing or too old #260
1 parent 1f979f5 commit 771e2ed

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

00-Preface.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@
722722
{
723723
"data": {
724724
"text/plain": [
725-
"[<matplotlib.lines.Line2D at 0x24ec4f535c0>]"
725+
"[<matplotlib.lines.Line2D at 0x1f04c819fc8>]"
726726
]
727727
},
728728
"execution_count": 18,
@@ -974,7 +974,7 @@
974974
"name": "python",
975975
"nbconvert_exporter": "python",
976976
"pygments_lexer": "ipython3",
977-
"version": "3.6.10"
977+
"version": "3.7.6"
978978
},
979979
"nbdime-conflicts": {
980980
"local_diff": [

book_format.py

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,95 @@
1919
from contextlib import contextmanager
2020
from IPython.core.display import HTML
2121
import json
22-
import matplotlib
23-
import matplotlib.pylab as pylab
24-
import matplotlib.pyplot as plt
25-
import numpy as np
2622
import os.path
2723
import sys
2824
import warnings
2925
from kf_book.book_plots import set_figsize, reset_figsize
3026

31-
# version 1.4.3 of matplotlib has a bug that makes
32-
# it issue a spurious warning on every plot that
33-
# clutters the notebook output
34-
if matplotlib.__version__ == '1.4.3':
35-
warnings.simplefilter(action="ignore", category=FutureWarning)
27+
def test_installation():
28+
try:
29+
import filterpy
30+
except:
31+
print("Please install FilterPy from the command line by running the command\n\t$ pip install filterpy\n\nSee chapter 0 for instructions.")
3632

37-
try:
38-
matplotlib.style.use('default')
39-
except:
40-
pass
33+
try:
34+
import numpy
35+
except:
36+
print("Please install NumPy before continuing. See chapter 0 for instructions.")
4137

42-
def test_filterpy_version():
38+
try:
39+
import scipy
40+
except:
41+
print("Please install SciPy before continuing. See chapter 0 for instructions.")
42+
43+
try:
44+
import sympy
45+
except:
46+
print("Please install SymPy before continuing. See chapter 0 for instructions.")
47+
48+
try:
49+
import matplotlib
50+
except:
51+
print("Please install matplotlib before continuing. See chapter 0 for instructions.")
4352

44-
import filterpy
4553
from distutils.version import LooseVersion
4654

4755
v = filterpy.__version__
4856
min_version = "1.4.4"
4957
if LooseVersion(v) < LooseVersion(min_version):
50-
raise Exception("Minimum FilterPy version supported is {}.\n"
51-
"Please install a more recent version.\n"
52-
" ex: pip install filterpy --upgrade".format(
58+
print("Minimum FilterPy version supported is {}. "
59+
"Please install a more recent version.\n"
60+
" ex: pip install filterpy --upgrade".format(
5361
min_version))
5462

5563

56-
# ensure that we have the correct filterpy loaded. This is
64+
v = matplotlib.__version__
65+
min_version = "1.5.0" # this is really old!!!
66+
if LooseVersion(v) < LooseVersion(min_version):
67+
print("Minimum Matplotlib version supported is {}. "
68+
"Please install a more recent version.".format(min_version))
69+
70+
# require Python 2.7, or 3.5+
71+
72+
import sys
73+
v = sys.version_info
74+
if v.major > 3:
75+
# I guess if we are this far in the future it'll work? Who knows.
76+
# I just don't want to start getting issue reports when Python goes
77+
# to 4.0
78+
return
79+
80+
if (v.major == 2 and v.minor != 7) or (v.major == 3 and v.minor < 5):
81+
print('You must use Python version 2.7 or 3.5+ for the notebooks to work correctly')
82+
83+
84+
# need to add test for IPython. I think I want to be at 6, which also implies
85+
# Python 3, matplotlib 2+, etc.
86+
87+
# ensure that we have the correct packages loaded. This is
5788
# called when this module is imported at the top of each book
58-
# chapter so the reader can see that they need to update FilterPy.
59-
test_filterpy_version()
89+
# chapter so the reader can see that they need to update their environment.
90+
test_installation()
91+
92+
93+
# now that we've tested the existence of all packages go ahead and import
94+
95+
import matplotlib
96+
import matplotlib.pylab as pylab
97+
import matplotlib.pyplot as plt
98+
import numpy as np
99+
100+
# version 1.4.3 of matplotlib has a bug that makes
101+
# it issue a spurious warning on every plot that
102+
# clutters the notebook output
103+
if matplotlib.__version__ == '1.4.3':
104+
warnings.simplefilter(action="ignore", category=FutureWarning)
105+
106+
try:
107+
matplotlib.style.use('default')
108+
except:
109+
pass
110+
60111

61112
with warnings.catch_warnings():
62113
warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
filterpy
2-
seaborn
32
jupyter
43
notebook
54
sympy

0 commit comments

Comments
 (0)