Discover fixes

Backport the fix for discovery in packages from Python bugtracker.
Right. Also - this is the upstream code.