Skip to content

Commit ff95455

Browse files
mrrytensorflower-gardener
authored andcommitted
Add a platform-dependent self-check that runs during import tensorflow.
This gives us the opportunity to raise actionable error messages (e.g. about missing DLLs on Windows) that are more useful than the loader errors we get from a failed SWIG import. PiperOrigin-RevId: 169315291
1 parent 76293c2 commit ff95455

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
16+
"""Platform-specific code for checking the integrity of the TensorFlow build."""
17+
from __future__ import absolute_import
18+
from __future__ import division
19+
from __future__ import print_function
20+
21+
import os
22+
23+
24+
def preload_check():
25+
"""Raises an exception if the environment is not correctly configured.
26+
27+
Raises:
28+
ImportError: If the check detects that the environment is not correctly
29+
configured, and attempting to load the TensorFlow runtime will fail.
30+
"""
31+
if os.name == "nt":
32+
# Attempt to load any DLLs that the Python extension depends on before
33+
# we load the Python extension, so that we can raise an actionable error
34+
# message if they are not found.
35+
import ctypes # pylint: disable=g-import-not-at-top
36+
try:
37+
ctypes.WinDLL("msvcp140.dll")
38+
except OSError:
39+
raise ImportError(
40+
"Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be "
41+
"installed in a directory that is named in your %PATH% environment "
42+
"variable. You may install this DLL by downloading Visual C++ 2015 "
43+
"Redistributable Update 3 from this URL: "
44+
"https://www.microsoft.com/en-us/download/details.aspx?id=53587")
45+
# TODO(mrry): Add specific checks for GPU DLLs if build_info indicates
46+
# that this is a GPU build.
47+
else:
48+
# TODO(mrry): Consider adding checks for the Linux and Mac OS X builds.
49+
pass

tensorflow/python/pywrap_tensorflow.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
import sys
2323
import traceback
2424

25+
from tensorflow.python.platform import self_check
26+
27+
28+
# Perform pre-load sanity checks in order to produce a more actionable error
29+
# than we get from an error during SWIG import.
30+
self_check.preload_check()
31+
2532
# pylint: disable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
2633

2734
# On UNIX-based platforms, pywrap_tensorflow is a SWIG-generated

0 commit comments

Comments
 (0)