code: def _run_code(code, code_path, ns=None, function_name=None):
      """
      Import a Python module from a path, and run the function given by
      name, if function_name is not None.
      """
  
      # Change the working directory to the directory of the example, so
      # it can get at its data files, if any.  Add its path to sys.path
      # so it can import any helper modules sitting beside it.
      pwd = os.getcwd()
      if setup.config.plot_working_directory is not None:
          try:
              os.chdir(setup.config.plot_working_directory)
          except OSError as err:
              raise OSError(
                  f"{err}\n`plot_working_directory` option in "
                  f"Sphinx configuration file must be a valid "
                  f"directory path"
              ) from err
          except TypeError as err:
              raise TypeError(
                  f"{err}\n`plot_working_directory` option in "
                  f"Sphinx configuration file must be a string or "
                  f"None"
              ) from err
      elif code_path is not None:
          dirname = os.path.abspath(os.path.dirname(code_path))
          os.chdir(dirname)
  
      with cbook._setattr_cm(
          sys, argv=[code_path], path=[os.getcwd(), *sys.path]
      ), contextlib.redirect_stdout(StringIO()):
          try:
              if ns is None:
                  ns = {}
              if not ns:
                  if setup.config.plot_pre_code is None:
                      exec(
                          "import numpy as np\n"
                          "from matplotlib import pyplot as plt\n",
                          ns,
                      )
                  else:
                      exec(str(setup.config.plot_pre_code), ns)
              if "__main__" in code:
                  ns["__name__"] = "__main__"
  
              # Patch out non-interactive show() to avoid triggering a warning.
              with cbook._setattr_cm(
                  FigureManagerBase, show=lambda self: None
              ):
                  exec(code, ns)
                  if function_name is not None:
                      exec(function_name + "()", ns)
  
          except (Exception, SystemExit) as err:
              raise PlotError(traceback.format_exc()) from err
          finally:
              os.chdir(pwd)
      return ns
  
imports: import contextlib
  import doctest
  from io import StringIO
  import itertools
  import os
  from os.path import relpath
  from pathlib import Path
  import re
  import shutil
  import sys
  import textwrap
  import traceback
  from docutils.parsers.rst import directives, Directive
  from docutils.parsers.rst.directives.images import Image
  import jinja2
  from sphinx.errors import ExtensionError
  import matplotlib
  from matplotlib.backend_bases import FigureManagerBase
  import matplotlib.pyplot
  from matplotlib import _pylab_helpers, cbook
label_desc: Use image node_id 10 for calling a global function with example usage: _run_code(code, code_path, ns, function_name) and returns: ns