docs: def open_package(locals=None, dr=None):
      """Try to open a package with the metatab_doc variable, which is set when a Notebook is run
      as a resource. If that does not exist, try the local _packages directory"""
  
  
      if locals is None:
          locals = caller_locals()
  
      try:
          # Running in a package build
          return op(locals['metatab_doc'])
  
      except KeyError:
          # Running interactively in Jupyter
  
          package_name = None
          build_package_dir = None
          source_package = None
  
          if dr is None:
              dr = getcwd()
  
          for i, e in enumerate(walk_up(dr)):
  
              intr = set([DEFAULT_METATAB_FILE, LINES_METATAB_FILE, IPYNB_METATAB_FILE]) & set(e[2])
  
              if intr:
  
                  source_package = join(e[0], list(intr)[0])
                  p = op(source_package)
                  package_name = p.find_first_value("Root.Name")
  
                  if not package_name:
                      raise PackageError("Source package in {} does not have root.Name term".format(e[0]))
  
                  if PACKAGE_PREFIX in e[1]:
                      build_package_dir = join(e[0], PACKAGE_PREFIX)
  
                  break
  
              if i > 2:
                  break
  
          if build_package_dir and package_name and exists(join(build_package_dir, package_name)):
              # Open the previously built package
              built_package = join(build_package_dir, package_name)
              try:
                  return op(built_package)
              except RowGeneratorError as e:
                  pass  # Probably could not open the metadata file.
  
          if source_package:
              # Open the source package
              return op(source_package)
  
      raise PackageError("Failed to find package, either in locals() or above dir '{}' ".format(dr))