from __future__ import division, print_function, with_statement

import inspect
import os


def determine_package(some_object):
    """
    Resolves a call by object to a call by package.
    - Determine absolute package name of the given object.
    - When the task gets executed, the worker process will import it.
    """

    # expand the module's path to an absolute import
    filename = inspect.getsourcefile(some_object)
    module_path, module_filename = os.path.split(filename)
    module_name, _ = os.path.splitext(module_filename)
    prefix = []
    for directory in module_path.split(os.sep)[::-1]:
        prefix.append(directory)
        candidate = ".".join(prefix[::-1] + [module_name])
        try:
            __import__(name=candidate, globals=globals(), locals=locals(),
                       fromlist=[], level=0)
            some_object = candidate
            return some_object
        except ImportError:
            pass
    raise ImportError("Could not determine the package of the given " +
                      "object. This should not happen.")

from __future__ import division, print_function, with_statement

import inspect
import os


def determine_package(some_object):
    """
    Resolves a call by object to a call by package.
    - Determine absolute package name of the given object.
    - When the task gets executed, the worker process will import it.
    """

    # expand the module's path to an absolute import
    filename = inspect.getsourcefile(some_object)
    module_path, module_filename = os.path.split(filename)
    module_name, _ = os.path.splitext(module_filename)
    prefix = []
    for directory in module_path.split(os.sep)[::-1]:
        prefix.append(directory)
        candidate = ".".join(prefix[::-1] + [module_name])

        if candidate.startswith("."):
            candidate = candidate[1:]

        try:
            __import__(name=candidate, globals=globals(), locals=locals(),
                       fromlist=[], level=0)
            some_object = candidate
            return some_object
        except ImportError:
            pass
    raise ImportError("Could not determine the package of the given " +
                      "object. This should not happen.")

Fix a bug (?) in detmine_package
Fix a bug (?) in detmine_package

Canditates have their first character removed if it is ".".

  from __future__ import division, print_function, with_statement
  
  import inspect
  import os
  
  
  def determine_package(some_object):
      """
      Resolves a call by object to a call by package.
      - Determine absolute package name of the given object.
      - When the task gets executed, the worker process will import it.
      """
  
      # expand the module's path to an absolute import
      filename = inspect.getsourcefile(some_object)
      module_path, module_filename = os.path.split(filename)
      module_name, _ = os.path.splitext(module_filename)
      prefix = []
      for directory in module_path.split(os.sep)[::-1]:
          prefix.append(directory)
          candidate = ".".join(prefix[::-1] + [module_name])
+ 
+         if candidate.startswith("."):
+             candidate = candidate[1:]
+ 
          try:
              __import__(name=candidate, globals=globals(), locals=locals(),
                         fromlist=[], level=0)
              some_object = candidate
              return some_object
          except ImportError:
              pass
      raise ImportError("Could not determine the package of the given " +
                        "object. This should not happen.")
  
Fix a bug (?) in detmine_package
## Code Before:
from __future__ import division, print_function, with_statement

import inspect
import os


def determine_package(some_object):
    """
    Resolves a call by object to a call by package.
    - Determine absolute package name of the given object.
    - When the task gets executed, the worker process will import it.
    """

    # expand the module's path to an absolute import
    filename = inspect.getsourcefile(some_object)
    module_path, module_filename = os.path.split(filename)
    module_name, _ = os.path.splitext(module_filename)
    prefix = []
    for directory in module_path.split(os.sep)[::-1]:
        prefix.append(directory)
        candidate = ".".join(prefix[::-1] + [module_name])
        try:
            __import__(name=candidate, globals=globals(), locals=locals(),
                       fromlist=[], level=0)
            some_object = candidate
            return some_object
        except ImportError:
            pass
    raise ImportError("Could not determine the package of the given " +
                      "object. This should not happen.")

## Instruction:
Fix a bug (?) in detmine_package
## Code After:
from __future__ import division, print_function, with_statement

import inspect
import os


def determine_package(some_object):
    """
    Resolves a call by object to a call by package.
    - Determine absolute package name of the given object.
    - When the task gets executed, the worker process will import it.
    """

    # expand the module's path to an absolute import
    filename = inspect.getsourcefile(some_object)
    module_path, module_filename = os.path.split(filename)
    module_name, _ = os.path.splitext(module_filename)
    prefix = []
    for directory in module_path.split(os.sep)[::-1]:
        prefix.append(directory)
        candidate = ".".join(prefix[::-1] + [module_name])

        if candidate.startswith("."):
            candidate = candidate[1:]

        try:
            __import__(name=candidate, globals=globals(), locals=locals(),
                       fromlist=[], level=0)
            some_object = candidate
            return some_object
        except ImportError:
            pass
    raise ImportError("Could not determine the package of the given " +
                      "object. This should not happen.")
