#!/usr/bin/python
import sys
import os.path

args = sys.argv[1:]

if len(args) == 0 or (set(args) & set("-h --help".split())):
    print "Usage: pywhich PYTHON_MODULE [PYTHON_MODULE]..."
    print ("Find the source file for PYTHON_MODULE(s),"
           "the result to standard out.")
    print
    print "If a .py file cannot be found then the modules bytecode file is found."
    sys.exit(0)

for name in args:
    module = reduce(getattr, name.split('.')[1:], __import__(name))
    if not hasattr(module, "__file__"):
        filename = "<builtin>"
    else:
        filename = module.__file__

    if filename.endswith('pyc'):
        py_file = filename[:-1]
        if os.path.exists(py_file):
            filename = py_file

    print filename
