8000 Upon user's `import plotly`, we check core/optional requirements. · Bobfrat/python-api@ed9be3e · GitHub
[go: up one dir, main page]

Skip to content

Commit ed9be3e

Browse files
committed
Upon user's import plotly, we check core/optional requirements.
The goal here is to both issue warnings when requirements are not installed and when requirements are not up to date.
1 parent 69ca33a commit ed9be3e

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

plotly/__init__.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
1+
import json
2+
import warnings
13
from . version import __version__
24
import graph_objs
35
import plotly
46
import tools
5-
import utils
7+
import utils
8+
9+
from pkg_resources import resource_string
10+
11+
12+
def _check_for_requirements():
13+
s = resource_string('plotly', 'requirements.json').decode('utf-8')
14+
15+
reqs = json.loads(s)
16+
17+
for req, version in reqs['core'].items():
18+
try:
19+
exec "import {}".format(req)
20+
except ImportError:
21+
warnings.warn(
22+
"{} is a core requirement, you'll have to install it "
23+
"on your machine before being able to use the plotly "
24+
"package.".format(req))
25+
else:
26+
our_version = version.split('.')
27+
exec "your_version = {}.__version__.split('.')".format(req)
28+
for ours, yours in zip(our_version, your_version):
29+
if int(ours) > int(yours):
30+
warnings.warn(
31+
"The core requirement, '{}', is on your "
32+
"machine, but it may be out of date. If you run "
33+
"into problems related to this package, "
34+
"you should try updating to the version we test "
35+
"with: '{}'. Your version: '{}'."
36+
"".format(req, version, ".".join(your_version)))
37+
38+
for req, version in reqs['optional'].items():
39+
try:
40+
exec "import {}".format(req)
41+
except ImportError:
42+
pass
43+
else:
44+
our_version = version.split('.')
45+
exec "your_version = {}.__version__.split('.')".format(req)
46+
for ours, yours in zip(our_version, your_version):
47+
if int(ours) > int(yours):
48+
warnings.warn(
49+
"The optional requirement, '{}', is on your "
50+
"machine, but it may be out of date. If you run "
51+
"into problems related to this package, "
52+
"you should try updating to the version we test "
53+
"with: '{}'. Your version: '{}'."
54+
"".format(req, version, ".".join(your_version)))
55+
56+
_check_for_requirements()

0 commit comments

Comments
 (0)
0