Print error if required package is missing or too old #260

This commit is contained in:
Roger Labbe 2020-05-04 17:09:28 -07:00
parent 1f979f509d
commit 771e2ed894
3 changed files with 75 additions and 25 deletions

View File

@ -722,7 +722,7 @@
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x24ec4f535c0>]"
"[<matplotlib.lines.Line2D at 0x1f04c819fc8>]"
]
},
"execution_count": 18,
@ -974,7 +974,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
"version": "3.7.6"
},
"nbdime-conflicts": {
"local_diff": [

View File

@ -19,15 +19,84 @@ from __future__ import (absolute_import, division, print_function,
from contextlib import contextmanager
from IPython.core.display import HTML
import json
import matplotlib
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import numpy as np
import os.path
import sys
import warnings
from kf_book.book_plots import set_figsize, reset_figsize
def test_installation():
try:
import filterpy
except:
print("Please install FilterPy from the command line by running the command\n\t$ pip install filterpy\n\nSee chapter 0 for instructions.")
try:
import numpy
except:
print("Please install NumPy before continuing. See chapter 0 for instructions.")
try:
import scipy
except:
print("Please install SciPy before continuing. See chapter 0 for instructions.")
try:
import sympy
except:
print("Please install SymPy before continuing. See chapter 0 for instructions.")
try:
import matplotlib
except:
print("Please install matplotlib before continuing. See chapter 0 for instructions.")
from distutils.version import LooseVersion
v = filterpy.__version__
min_version = "1.4.4"
if LooseVersion(v) < LooseVersion(min_version):
print("Minimum FilterPy version supported is {}. "
"Please install a more recent version.\n"
" ex: pip install filterpy --upgrade".format(
min_version))
v = matplotlib.__version__
min_version = "1.5.0" # this is really old!!!
if LooseVersion(v) < LooseVersion(min_version):
print("Minimum Matplotlib version supported is {}. "
"Please install a more recent version.".format(min_version))
# require Python 2.7, or 3.5+
import sys
v = sys.version_info
if v.major > 3:
# I guess if we are this far in the future it'll work? Who knows.
# I just don't want to start getting issue reports when Python goes
# to 4.0
return
if (v.major == 2 and v.minor != 7) or (v.major == 3 and v.minor < 5):
print('You must use Python version 2.7 or 3.5+ for the notebooks to work correctly')
# need to add test for IPython. I think I want to be at 6, which also implies
# Python 3, matplotlib 2+, etc.
# ensure that we have the correct packages loaded. This is
# called when this module is imported at the top of each book
# chapter so the reader can see that they need to update their environment.
test_installation()
# now that we've tested the existence of all packages go ahead and import
import matplotlib
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import numpy as np
# version 1.4.3 of matplotlib has a bug that makes
# it issue a spurious warning on every plot that
# clutters the notebook output
@ -39,24 +108,6 @@ try:
except:
pass
def test_filterpy_version():
import filterpy
from distutils.version import LooseVersion
v = filterpy.__version__
min_version = "1.4.4"
if LooseVersion(v) < LooseVersion(min_version):
raise Exception("Minimum FilterPy version supported is {}.\n"
"Please install a more recent version.\n"
" ex: pip install filterpy --upgrade".format(
min_version))
# ensure that we have the correct filterpy loaded. This is
# called when this module is imported at the top of each book
# chapter so the reader can see that they need to update FilterPy.
test_filterpy_version()
with warnings.catch_warnings():
warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)

View File

@ -1,5 +1,4 @@
filterpy
seaborn
jupyter
notebook
sympy