# Can a file know if it has been \include'ed or \input'ed?

I have a somewhat large User's Guide for a code I've been working on with collaborators. Each chapter is in a subdirectory of the top directory, which includes the .tex file that grabs and stitches together all the individual chapter .tex files into the complete document.

Sometimes it is useful to compile and look at just one chapter individually. This got me curious to see if (La)TeX can handle the following: Can (La)TeX determine during compilation if a .tex file was \input'ed or \include'ed from another file?

An example use case would be to only include a \documentclass, \begin{document}, etc. if the file were not \include'ed or \input'ed from another file. Yes, I understand this can be worked around in other ways, i.e. smart use of \includeonly, but I'm curious if this can be done in a more automatic fashion.

For those familiar with Python, what I'm really looking for is something equivalent to the usual

if __name__ == "__main__":

check to see if the current file is the main module, or if it has been import'ed by another file.

-
Possibly related: About \include in LaTeX –  Werner Aug 31 '11 at 17:31