Repository: r1chardj0n3s/pip-check-reqs

Issue:
Version 2.5.0 fails in importlib with ValueError: "__main__.__spec__ is None"
Hi, The newly released version 2.5.0 fails when checking ansible against our minimum constraints file:

```
$ pip-missing-reqs $(python -c "import ansible,os; print(os.path.dirname(ansible.__file__))") --requirements-file=minimum-constraints.txt
Traceback (most recent call last):
  File "/Users/maiera/virtualenvs/zhmcam39/bin/pip-missing-reqs", line 8, in <module>
    sys.exit(main())
  File "/Users/maiera/virtualenvs/zhmcam39/lib/python3.9/site-packages/pip_check_reqs/find_missing_reqs.py", line 201, in main
    missing = find_missing_reqs(
  File "/Users/maiera/virtualenvs/zhmcam39/lib/python3.9/site-packages/pip_check_reqs/find_missing_reqs.py", line 35, in find_missing_reqs
    used_modules = common.find_imported_modules(
  File "/Users/maiera/virtualenvs/zhmcam39/lib/python3.9/site-packages/pip_check_reqs/common.py", line 138, in find_imported_modules
    vis.visit(ast.parse(content, str(filename)))
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 415, in generic_visit
    self.visit(item)
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)
  File "/Users/maiera/virtualenvs/zhmcam39/lib/python3.9/site-packages/pip_check_reqs/common.py", line 59, in visit_Import
    self._add_module(alias.name, node.lineno)
  File "/Users/maiera/virtualenvs/zhmcam39/lib/python3.9/site-packages/pip_check_reqs/common.py", line 79, in _add_module
    find_spec_result = importlib.util.find_spec(
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/util.py", line 114, in find_spec
    raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None
```

This happened on my local system on Python 3.9 after an upgrade of packages to the latest released versions. It happens also in our CI environment on Python 3.9, 3.10, 3.11.

The difference in pip list output before and after the upgrade is:
```
(zhmcam39) Andreass-MBP-2:zhmc-ansible-modules maiera$ diff list1.out list2.out
48c48
< GitPython                         3.1.34
---
> GitPython                         3.1.35
70c70
< packaging                         23.1
---
> packaging                         21.3
74c74
< pip-check-reqs                    2.4.4
---
> pip-check-reqs                    2.5.0
92c92
< pytest                            7.4.1
---
> pytest                            7.4.2
114c114
< setuptools                        68.1.2
---
> setuptools                        68.2.0
```

Downgrading just pip-check-reqs to 2.4.4 again makes the error disappear again, so I'm quite certain it is related to pip-check-reqs 2.5.0.

I did not check whether the issue occurs for packages other than Ansible.

Reproduction:
* get our minimum-constraints.txt file from https://github.com/zhmcclient/zhmc-ansible-modules/blob/master/minimum-constraints.txt
* Set up and activate a virtual Python 3.9 or higher environment 
* Install ansible: `pip install ansible`
* Install pip-check-reqs: `pip install pip-check-reqs==2.5.0`
* Run `pip-missing-reqs $(python -c "import ansible,os; print(os.path.dirname(ansible.__file__))") --requirements-file=minimum-constraints.txt` 

Circumvention: Excluding pip-check-reqs 2.5.0 in the dependencies - hoping that 2.5.1 will solve it :-)

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index f23906e..9e20c33 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,10 @@
 Release History
 ---------------
 
+2.5.1
+
+- Fix an issue with importing `__main__`.
+
 2.5.0
 
 - Support Python 3.10.
diff --git a/pip_check_reqs/common.py b/pip_check_reqs/common.py
index 70bb9b5..4410c3f 100644
--- a/pip_check_reqs/common.py
+++ b/pip_check_reqs/common.py
@@ -76,9 +76,15 @@ class _ImportVisitor(ast.NodeVisitor):
         if self._ignore_modules_function(modname):
             return
 
-        module_spec = importlib.util.find_spec(
-            name=modname.split(".")[0],
-        )
+        try:
+            module_spec = importlib.util.find_spec(
+                name=modname.split(".")[0],
+            )
+        except ValueError:
+            # The module has no __spec__ attribute.
+            # For example, if importing __main__.
+            return
+
         if module_spec is None:
             # The component specified at this point is not installed.
             return


Test patch:
diff --git a/tests/test_common.py b/tests/test_common.py
index af060cd..6a73c26 100644
--- a/tests/test_common.py
+++ b/tests/test_common.py
@@ -142,6 +142,10 @@ def test_find_imported_modules(
         from __future__ import braces
         import ast, sys
         from . import friend
+
+        # __main__ is a special case -  sys.modules["__main__"] has no __spec__
+        # attribute.
+        import __main__
         """,
     )
     ham_file_contents = textwrap.dedent(