Discover fixes

Backport the fix for discovery in packages from Python bugtracker.
Thispart is upstream unaltered code - I don't want any variance in this code (other than changing to match imports) - it will make reconciling harder. That said, the else branch is for when the import succeeds.