import os

def a():
   print("A")
def b():
   print("B")
# I want to sometimes skip the main body
if os.path.isdir("C:\\Blender\\mainbody"):
   print("main body of module_A")

Is there a way to control whether or not the main body of a module gets executed by passing in arguments to the import command, such as

import module_A "mainbody"

Right now I am controlling it by checking for existence of a directory, I know I could also open some kind of config file which is essentially the same approach but how about just passing in an argument along with import xxx ????

You can write a module that can also act as a script by using the idiom:

if __name__ == '__main__':
    pass

Importing a module and then executing the main body of that module is indicative of a design problem, but can be accomplished by wrapping the main body in a main function.

#module.py

def main():
    pass

if __name__ == '__main__':
    main()

#script.py

import module

module.main()

You should test for whatever in the program, and if true then call the function or class in the imported module. As stated previously, this is a design problem not a programming problem. A simple example from you would help.

I am writing a script in Blender 3D, which (Blender) uses a built in Python interpreter as its API. There's too much code to paste here but essentially the script does a 3D mesh manipulation. Because I have not found any info yet on how to debug interactively, I run from console and break the script into as many small functions as possible for debugging (vertex selection, vertex transform, etc..). Sometimes I'll modify 2 or 3 funtions, then reload the module which runs main and all sub funtcions. Sometimes I reload the module but just want to test one or two sub-functions manually without calling main. I was trying to (at the python command line) control how I import the module i.e. "imp.reload(xxx) run_main" or imp.reload(xxx) dont_run main" then "xxx.f1()" and "xxx.f5()" etc...

You should always take small steps and add functionality only after fully tested last one to know that it is reasonably sure that the bug in code is in the last code added.

General philosophy of testing is geared toward defining test cases for the functions and running those test cases after all changes to them.

Thank you for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.