Python3 - How do I import a class from another file

Terry Reedy tjreedy at udel.edu
Mon Dec 9 05:32:47 EST 2019


On 12/9/2019 2:36 AM, R.Wieser wrote:
> Terry,
>> Standard for in-file test is
> [snip]
> 
> Any reason to put the testcode in a procedure instead of just have it
> directly follow the "if __name__ == '__main__' " test ?

One can, for instance, interactively do

 >>> import mod
 >>> mod.test()

and explore the contents of mod, perhaps depending on the result of test()

def test(): ... also marks the code as test code rather than, for 
instance, command-line or setup code.  Putting such alternative code in 
function 'main' has the same effect.

For instance, 3.x has idlelib.__main__, which has something like

from idlelib.pyshell import main
main()

so than one can run 3.x IDLE from a command line with 'python -m idlelib'.

-- 
Terry Jan Reedy



More information about the Python-list mailing list