Package: demain
This package provides a clean solution for the case where you want to
write a Python module that can safely be invoked from the command line
but which also needs to be imported by other modules.  Simply write each
module that would normally make a__name__=='__main__'check like this instead:'Your module docstring'

import demain
__main__ = demain.fix()

# Your other import statements
# Your classes and functions

if __main__:
    # Your code that gets invoked from the command lineIn cases where your module has simply been imported by another module,
thefix()function does nothing and returnsFalse.  But if your
module has been invoked directly, either as a script or using Python’s-moption, thenfix()will:Replace your module’s__name__global variable with its true name.Re-register your module insys.modulesunder its true name, so that
laterimportstatements by other Python modules can find it.If your module is beneath a package, then the module is inserted into
the package under its correct name.After making these corrections, the function returnsTrueso that
your module can detect that it is running as the main program.I advocate that a future version of Python stop mangling module names
altogether, and instead set a__main__boolean in every module likedemaindoes.  But until then, usedemainto bring your programs
some sanity, safely, and fewer mysterious breakages and error messages.BackgroundWhen you run a Python script from the command line, or invoke a module
using the-moption, Python gives that script or module the fake
name'__main__'so that it can detect that it is running as the
“main program.”  Consider a small script that looks like this:# foo.py
print __name__Invoking it from the command line obliterates the normalfooname
which it would have otherwise (as we can demonstrate, by importing the
module from the command line):$ python foo.py
__main__
$ python -m foo
__main__
$ python -c 'import foo'
fooThis name mangling causes no end of trouble when used with modules that
themselves are targets of importation, because the first attempt to
import the module under its real name results in a duplicate of the
module being created, with its own copy of every object and class.  The
bugs that this causes tend to be particularly difficult to track down.Thedemain.fix()function is designed to prevent this problem from
even starting, by being the very first thing that you call at the top of
every module that you intend to be invoked directly.  Be careful not
only to avoid doing other imports before before runningfix()but
also make sure that you create no classes first; otherwise, the name'__main__'will get copied into your classes as their module’s name.I knew that I finally had to do something about this problem when I readDefending Pyramid’s Designand reached Chris McDonough’s anguished
plea in the section titled, “Application Programmers Don’t Control The
Module-Scope Codepath.”http://docs.pylonshq.com/pyramid/dev/designdefense.htmlThere, his long experience with Python and his sharply-honed coding
practices lead him to make some excellent recommendations against the
dangers that small Python applications — and in particular those written
using Web micro-frameworks — incur when they treat a script as though it
were a normal module.  My hope is thatdemainwill solve this
problem by making an invoked Python script a safe place for objects,
instead of having them all duplicated when the same Python file is
imported from elsewhere in the application using a normal import.This package comes with a modest test suite, built from a few hand-built
examples, as well as McDonough’s sample decorator framework that shows
the problem with Python’s handling of script invocation.  To run it,
simply type:$ python -m demain.tests