Check for relative imports first.

Also fixes a slightly obscure issue where trying to relative-import the
same name a second time from a different directory would be ignored,
because the cache works by name rather than full path.