bpo-27200: Fix several doctests (GH-604)

diff --git a/Doc/library/email.compat32-message.rst b/Doc/library/email.compat32-message.rst
index b070764..2394e4d 100644
--- a/Doc/library/email.compat32-message.rst
+++ b/Doc/library/email.compat32-message.rst
@@ -660,10 +660,14 @@
 
       .. testsetup::
 
-         >>> from email import message_from_binary_file
-         >>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
-         ...     msg = message_from_binary_file(f)
-         >>> from email.iterators import _structure
+         import email
+         from email import message_from_binary_file
+         from os.path import join, dirname
+         lib_dir = dirname(dirname(email.__file__))
+         file_path = join(lib_dir, 'test/test_email/data/msg_16.txt')
+         with open(file_path, 'rb') as f:
+             msg = message_from_binary_file(f)
+         from email.iterators import _structure
 
       .. doctest::
 
@@ -686,7 +690,7 @@
       .. doctest::
 
          >>> for part in msg.walk():
-         ...     print(part.get_content_maintype() == 'multipart'),
+         ...     print(part.get_content_maintype() == 'multipart',
          ...           part.is_multipart())
          True True
          False False
@@ -698,11 +702,11 @@
          >>> _structure(msg)
          multipart/report
              text/plain
-         message/delivery-status
-             text/plain
-             text/plain
-         message/rfc822
-             text/plain
+             message/delivery-status
+                 text/plain
+                 text/plain
+             message/rfc822
+                 text/plain
 
       Here the ``message`` parts are not ``multiparts``, but they do contain
       subparts. ``is_multipart()`` returns ``True`` and ``walk`` descends
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 6621f4a..925da50 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -331,7 +331,7 @@
    The resulting list is sorted alphabetically.  For example:
 
       >>> import struct
-      >>> dir()   # show the names in the module namespace
+      >>> dir()   # show the names in the module namespace  # doctest: +SKIP
       ['__builtins__', '__name__', 'struct']
       >>> dir(struct)   # show the names in the struct module # doctest: +SKIP
       ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst
index 90dfd46..36c6859 100644
--- a/Doc/library/ipaddress.rst
+++ b/Doc/library/ipaddress.rst
@@ -25,9 +25,11 @@
 .. versionadded:: 3.3
 
 .. testsetup::
-   >>> import ipaddress
-   >>> from ipaddress import (ip_network, IPv4Address, IPv4Interface,
-   ...                        IPv4Network)
+
+   import ipaddress
+   from ipaddress import (
+       ip_network, IPv4Address, IPv4Interface, IPv4Network,
+   )
 
 Convenience factory functions
 -----------------------------
diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst
index 0905b98..5149bcf 100644
--- a/Doc/library/reprlib.rst
+++ b/Doc/library/reprlib.rst
@@ -48,6 +48,7 @@
    same thread.  If a recursive call is made, the *fillvalue* is returned,
    otherwise, the usual :meth:`__repr__` call is made.  For example:
 
+        >>> from reprlib import recursive_repr
         >>> class MyList(list):
         ...     @recursive_repr()
         ...     def __repr__(self):
diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst
index 55012f8..fb335c6 100644
--- a/Doc/library/shlex.rst
+++ b/Doc/library/shlex.rst
@@ -43,15 +43,16 @@
    string that can safely be used as one token in a shell command line, for
    cases where you cannot use a list.
 
-   This idiom would be unsafe::
+   This idiom would be unsafe:
 
       >>> filename = 'somefile; rm -rf ~'
       >>> command = 'ls -l {}'.format(filename)
       >>> print(command)  # executed by a shell: boom!
       ls -l somefile; rm -rf ~
 
-   :func:`quote` lets you plug the security hole::
+   :func:`quote` lets you plug the security hole:
 
+      >>> from shlex import quote
       >>> command = 'ls -l {}'.format(quote(filename))
       >>> print(command)
       ls -l 'somefile; rm -rf ~'
@@ -61,6 +62,7 @@
 
    The quoting is compatible with UNIX shells and with :func:`split`:
 
+      >>> from shlex import split
       >>> remote_command = split(remote_command)
       >>> remote_command
       ['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
index 7a5b56f..6754e26 100644
--- a/Doc/library/urllib.parse.rst
+++ b/Doc/library/urllib.parse.rst
@@ -64,6 +64,9 @@
    input is presumed to be a relative URL and thus to start with
    a path component.
 
+   .. doctest::
+      :options: +NORMALIZE_WHITESPACE
+
        >>> from urllib.parse import urlparse
        >>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
        ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',