Fix how we detect the TF package #24255
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Our framework detection code calls
_is_package_available()for TensorFlow, but this code fails when only thetensorflow-cpupackage is present. The failure occurs becauseimportlib_metadata.version("tensorflow")throws an error in the version detection branch of_is_package_availableunless the coretensorflowpackage is installed.I solved this by just calling
importlib.util.find_spec("tensorflow")instead of_is_package_available(). However, we could also resolve this issue by rewriting_is_package_available()so that it only takes the version check branch whenreturn_versionisTrue. Theimportlib_metadata.version()call is only used to get the package version, but it causes the entire_is_package_available()call to fail if it can't find a version, even if theimportlib.util.find_spec()call was a success.ccing @sgugger because there's a
TODOabove that function requesting his attention, so I'd like his input on the right approach here!Fixes #24253