Repository: scipy/scipy

Issue:
DEV: `dev.py` missing PYTHONPATH when building doc
I noticed that the PYTHONPATH was not set when building doc.

I need to do this for it to work
```
export PYTHONPATH=$PWD/build-install/lib/python3.10/site-packages
python dev.py -n -j2 --doc html-scipyorg
```

cc @Smit-create 


Discussion:
So what is broken is `python dev.py --doc`.
> So what is broken is `python dev.py --doc`.

I think it should check if SciPy is importable, if not it should try to add it to the PYTHONPATH. I actually see that other commands don't do this too (although written in the help: `Start IPython shell with PYTHONPATH set`. For me this means that it does the job of finding the correct export path for me and exporting it). ~This was the behaviour with `runtests.py` no?~ mmm no but was not needed since we had inplace builds.
I believe this is the same issue as addressed here: https://github.com/scipy/scipy/pull/15440. We can use the same fix for docs too. That works for me:
```
$ python dev.py -n --doc
Trying to import scipy from development installed path at: /home/admin-pc/Smitlunagariya/scipy/build-install/lib/python3.10/site-packages
make: Entering directory '/home/admin-pc/Smitlunagariya/scipy/doc'
mkdir -p build/html build/doctrees
LANG=C "/home/admin-pc/anaconda3/envs/scipy-dev/bin/python" -msphinx -WT --keep-going -t scipyorg  -b html -d build/doctrees  "-j1" source build/html-scipyorg 
Running Sphinx v4.3.1
SciPy (VERSION 1.9.0.dev0+1511.225c99f)
making output directory... done

```
That sounds right. Just to make sure @Smit-create, are you saying that it already works for you now, or you have a local fix to make it work?

Resolving patch:
diff --git a/dev.py b/dev.py
--- a/dev.py
+++ b/dev.py
@@ -223,6 +223,8 @@ def main(argv):
         sys.path.insert(0, site_dir)
         os.environ['PYTHONPATH'] = \
             os.pathsep.join((site_dir, os.environ.get('PYTHONPATH', '')))
+    else:
+        test, version, mod_path = get_project_info()
 
     extra_argv = args.args[:]
     if extra_argv and extra_argv[0] == '--':
@@ -337,29 +339,6 @@ def main(argv):
 
     if args.build_only:
         sys.exit(0)
-    else:
-        try:
-            __import__(PROJECT_MODULE)
-            test = sys.modules[PROJECT_MODULE].test
-            version = sys.modules[PROJECT_MODULE].__version__
-            mod_path = sys.modules[PROJECT_MODULE].__file__
-            mod_path = os.path.abspath(os.path.join(os.path.dirname(mod_path)))
-        except ImportError:
-            current_python_path = os.environ.get('PYTHONPATH', None)
-            print("Unable to import {} from: {}".format(PROJECT_MODULE,
-                                                       current_python_path))
-            site_dir = get_site_packages()
-            print("Trying to import scipy from development installed path at:",
-                  site_dir)
-            sys.path.insert(0, site_dir)
-            os.environ['PYTHONPATH'] = \
-                os.pathsep.join((site_dir, os.environ.get('PYTHONPATH', '')))
-            __import__(PROJECT_MODULE)
-            test = sys.modules[PROJECT_MODULE].test
-            version = sys.modules[PROJECT_MODULE].__version__
-            mod_path = sys.modules[PROJECT_MODULE].__file__
-            mod_path = os.path.abspath(os.path.join(os.path.dirname(mod_path)))
-
 
     if args.submodule:
         tests = [PROJECT_MODULE + "." + args.submodule]
@@ -403,6 +382,28 @@ def main(argv):
         sys.exit(1)
 
 
+def get_project_info():
+    """
+    Function to import the project module and return its tests, version,
+    and path where it is found.
+    If the project module is not found, then it tries to find it in the
+    development installed path.
+    """
+    try:
+        test, version, mod_path = runtests.import_module()
+    except ImportError:
+        # this may fail when running with --no-build, so try to detect
+        # an installed scipy in a subdir inside a repo
+        site_dir = get_site_packages()
+        print("Trying to find scipy from development installed "
+              "path at:", site_dir)
+        sys.path.insert(0, site_dir)
+        os.environ['PYTHONPATH'] = \
+            os.pathsep.join((site_dir, os.environ.get('PYTHONPATH', '')))
+        test, version, mod_path = runtests.import_module()
+    return test, version, mod_path
+
+
 def setup_build(args, env):
     """
     Setting up meson-build


Test patch: