Repository: networkx/networkx

Issue:
backend nx-loopback not found when trying to run pytest
In some configurations (that I haven't been able to track down) I can't use pytest with networkx.

I've traced the problem to [line 55 in conftest.py](https://github.com/networkx/networkx/blob/46d67fecfec615215cfa8b26e5024ae743e5973a/networkx/conftest.py#L55) where the `nx-loopback` entry_point is obtained and stored in the backends dict.  The problem arises when the `entry_points()` call returns an empty list.  The next line then tries to iterate to find the first element of the list, but gets a StopIteration instead.

I'm thinking that we need to add a check there for finding no entry point and handle it in some way. But I don't know enough about entry points to figure out what to do. And I'm guessing a new developer trying to test their added function to networkx wouldn't know what to do either.  

Do I have to do something with my environment to turn on the networkx entry_points? Do I have to turn on entry_points to run pytest on my local repo?

<details>

```
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/_pytest/main.py", line 265, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1046, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/pluggy/_hooks.py", line 514, in call_historic
INTERNALERROR>     res = self._hookexec(self.name, self._hookimpls, kwargs, False)
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/pluggy/_manager.py", line 115, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/pluggy/_callers.py", line 113, in _multicall
INTERNALERROR>     raise exception.with_traceback(exception.__traceback__)
INTERNALERROR>   File "/Users/dschult/mambaforge/lib/python3.10/site-packages/pluggy/_callers.py", line 77, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/dschult/NX/NXmyshort/networkx/conftest.py", line 56, in pytest_configure
INTERNALERROR>     networkx.utils.backends.backends["nx-loopback"] = next(iter(backends))
INTERNALERROR> StopIteration
```
</details>



Discussion:
> In some configurations (that I haven't been able to track down) I can't use pytest with networkx.

I've noticed this as well - one thing I've noticed that may or may not be relevant is that re-running `pip install -e .` usually fixes the problem. Perhaps there's something related to the entry points not playing nice with editable installs?
After recreatig this scenario a bunch I have determined that my workflow runs into this when I have `PYTHONPATH` pointing to a pre-nx-loopback version of networkx while I am testing in a post-nx-loopback repo. This matches what @rossbar describes as testing one repo when another version is installed.

I think all is well with the following change:
```python
    if backends:
        nx.utils.backends.backends["nx-loopback"] = next(iter(backends))
 ```
No exception is raised at the load staged -- only once any test that involves the backend runs. (I get 5 tests failing in these conditions.)  

If you think it is better, we can either give a warning (which prints before the tests are run).
Or we could raise an exception making us fix things before being able to test.
I'm slightly in favor of giving a warning.  Something like:
```python
    if backends:
        nx.utils.backends.backends["nx-loopback"] = next(iter(backends))
    else:
        warnings.warn(
            "\n\n             WARNING: Mixed NetworkX configuration! \n\n"
            "        This environment has mixed configuration for networkx.\n"
            "        The test object nx-loopback is not configured correctly.\n"
            "        You should not be seeing this message.\n"
            "        Try `pip install -e .`, or change your PYTHONPATH\n"
            "        Make sure you are testing a repo that mirrors entry_points\n"
            "        of the networkx library found by python.\n\n"
        )   
```
But perhaps this is too long. :}

Resolving patch:
diff --git a/networkx/conftest.py b/networkx/conftest.py
--- a/networkx/conftest.py
+++ b/networkx/conftest.py
@@ -53,7 +53,17 @@ def pytest_configure(config):
         networkx.utils.backends._dispatch._fallback_to_nx = bool(fallback_to_nx)
     # nx-loopback backend is only available when testing
     backends = entry_points(name="nx-loopback", group="networkx.backends")
-    networkx.utils.backends.backends["nx-loopback"] = next(iter(backends))
+    if backends:
+        networkx.utils.backends.backends["nx-loopback"] = next(iter(backends))
+    else:
+        warnings.warn(
+            "\n\n             WARNING: Mixed NetworkX configuration! \n\n"
+            "        This environment has mixed configuration for networkx.\n"
+            "        The test object nx-loopback is not configured correctly.\n"
+            "        You should not be seeing this message.\n"
+            "        Try `pip install -e .`, or change your PYTHONPATH\n"
+            "        Make sure python finds the networkx repo you are testing\n\n"
+        )
 
 
 def pytest_collection_modifyitems(config, items):
diff --git a/networkx/lazy_imports.py b/networkx/lazy_imports.py
--- a/networkx/lazy_imports.py
+++ b/networkx/lazy_imports.py
@@ -95,7 +95,7 @@ def __getattr__(self, x):
                 f"No module named '{fd['spec']}'\n\n"
                 "This error is lazily reported, having originally occurred in\n"
                 f'  File {fd["filename"]}, line {fd["lineno"]}, in {fd["function"]}\n\n'
-                f'----> {"".join(fd["code_context"]).strip()}'
+                f'----> {"".join(fd["code_context"] or "").strip()}'
             )
 
 


Test patch: