Repository: pytorch
Issue: modulefinder_determinator is incompatible with imports into the test/ directory
Labels: module: tests,triaged

## 🐛 Bug

`modulefinder_determinator.should_run_test` raises `AttributeError` if it runs on a test file that has local imports.

This is a specific manifestation of a known issue (see warning in https://github.com/pytorch/pytorch/blob/master/tools/testing/modulefinder_determinator.py#L201).

## To Reproduce

Steps to reproduce the behavior:
```
~/src/pytorch/test$ python. # note that we are in the test/ subdir
Python 3.9.6 (default, Jul 30 2021, 16:35:19) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> 
>>> from tools.testing import modulefinder_determinator
>>> 
>>> Options = collections.namedtuple('Options', 'verbose')
>>> 
>>> modulefinder_determinator.should_run_test(['test_ao_sparsity'], 'test_ao_sparsity', [], Options(True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/fsx/users/mikeyd/src/pytorch/tools/testing/modulefinder_determinator.py", line 101, in should_run_test
    dep_modules = get_dep_modules(test)
  File "/fsx/users/mikeyd/src/pytorch/tools/testing/modulefinder_determinator.py", line 214, in get_dep_modules
    finder.run_script(str(test_location))
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 165, in run_script
    self.load_module('__main__', fp, pathname, stuff)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 361, in load_module
    self.scan_code(co, m)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 434, in scan_code
    self._safe_import_hook(name, m, fromlist, level=0)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 379, in _safe_import_hook
    self.import_hook(name, caller, level=level)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 177, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 233, in find_head_package
    q = self.import_module(head, qname, parent)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 320, in import_module
    fp, pathname, stuff = self.find_module(partname,
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 512, in find_module
    return _find_module(name, path)
  File "/fsx/users/mikeyd/conda/envs/pytorch/lib/python3.9/modulefinder.py", line 81, in _find_module
    if spec.loader.is_package(name):
AttributeError: 'NoneType' object has no attribute 'is_package'
```

## Expected behavior
Does not raise. If it can't figure it out, default to running the test.

## Environment

This seems to be exclusive to Python 3.9 but it does unfortunately trigger in CI.

## Additional context
This caused a lot of pain with two PRs that wanted to add local imports to test_autograd.py.
#63400 and #63947

I think a short-term improvement could be to catch AttributeError and print more details about what is going on and point to workarounds.

This is very likely to come up again.

cc @mruberry