Is it possible for my Mercurial hook to call code from another file?
Question: I have a hook function named `precommit_bad_branch` which imports
`hook_utils`. When invoking `precommit_bad_branch` via a commit I get the
following error message:

    
    
    error: precommit.branch_check hook raised an exception: No module named hook_utils
    abort: No module named hook_utils!
    

It looks like I'm not allowed to call `hook_utils` from
`precommit_bad_branch`. The code works fine if I call it explicitly without
involving Mercurial.

Is it possible for my hook to call code from another file?

My hgrc hook part looks like this:

    
    
    [hooks]
    precommit.branch_check = python:C:\workspaces\hg_hooks\next_hooks.py:precommit_bad_branch
    precommit.debug_code_check = python:C:\workspaces\hg_hooks\common_hooks.py:precommit_contains_debug_code
    preupdate.merge_check = python:C:\workspaces\hg_hooks\next_hooks.py:preupdate_bad_merge
    


Answer: Put the `C:\workspaces\hg_hooks` directory in your `PYTHONPATH` and you will
be able to write

    
    
    [hooks]
    precommit.branch_check = python:next_hooks.precommit_bad_branch
    

in your configuration file and you will also be able to do

    
    
    import hook_utils
    

inside any Python file, including the `next_hooks.py` file.

Alternatively, you can modify `sys.path` from `next_hooks.py`, perhaps with
code like this:

    
    
    import sys
    import os
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    
    import hook_utils
    

