Repository: pandas-dev/pandas

Issue:
Unable to Build Documentation
```bash

python make.py html

Traceback (most recent call last):
  File "doc/make.py", line 23, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'
```

I believe a slight bug was introduced by c8859b57b891701f250fb05f2cc60d2e6cae2d6b in including `import pandas` at the top of the script. Unless the user has pandas installed as a package I don't think this module would know where to find it without modifying the path or being more explicit on import

@jorisvandenbossche 


Discussion:
The path is being modified somewhere else in the file make.py. If you inline the import inside the `_process_single_doc` method, does that fix the error?
I don't think that makes a difference. I looked but didn't see anywhere else in the file that added the project root to the import path - perhaps I'm overlooking it?

Otherwise adding something like the below fixes the issue:

```python
DOC_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(DOC_PATH))
import pandas
```
https://github.com/pandas-dev/pandas/blob/master/doc/make.py#L346

and the default is '..'
Can you actually try it? I hope it would work because it then comes after `os.environ['PYTHONPATH'] = args.python_path`, but not fully sure imports work that way.
Hmm OK I see what we are trying to do. I think setting `os.environ['PYTHONPATH']` gives sphinx access to the import path when it compiles the module externally, but doesn't help the import mechanism specifically within the module. 

I'll push a PR in a few that I think should serve both purposes

Resolving patch:
diff --git a/doc/make.py b/doc/make.py
--- a/doc/make.py
+++ b/doc/make.py
@@ -11,6 +11,7 @@
     $ python make.py html
     $ python make.py latex
 """
+import importlib
 import sys
 import os
 import shutil
@@ -20,8 +21,6 @@
 import webbrowser
 import jinja2
 
-import pandas
-
 
 DOC_PATH = os.path.dirname(os.path.abspath(__file__))
 SOURCE_PATH = os.path.join(DOC_PATH, 'source')
@@ -134,7 +133,7 @@ def _process_single_doc(self, single_doc):
             self.single_doc = single_doc
         elif single_doc is not None:
             try:
-                obj = pandas
+                obj = pandas  # noqa: F821
                 for name in single_doc.split('.'):
                     obj = getattr(obj, name)
             except AttributeError:
@@ -332,7 +331,7 @@ def main():
                                  'compile, e.g. "indexing", "DataFrame.join"'))
     argparser.add_argument('--python-path',
                            type=str,
-                           default=os.path.join(DOC_PATH, '..'),
+                           default=os.path.dirname(DOC_PATH),
                            help='path')
     argparser.add_argument('-v', action='count', dest='verbosity', default=0,
                            help=('increase verbosity (can be repeated), '
@@ -343,7 +342,13 @@ def main():
         raise ValueError('Unknown command {}. Available options: {}'.format(
             args.command, ', '.join(cmds)))
 
+    # Below we update both os.environ and sys.path. The former is used by
+    # external libraries (namely Sphinx) to compile this module and resolve
+    # the import of `python_path` correctly. The latter is used to resolve
+    # the import within the module, injecting it into the global namespace
     os.environ['PYTHONPATH'] = args.python_path
+    sys.path.append(args.python_path)
+    globals()['pandas'] = importlib.import_module('pandas')
 
     builder = DocBuilder(args.num_jobs, not args.no_api, args.single,
                          args.verbosity)


Test patch: