In Python I have a module myModule.py where I define a few functions and a main(), which takes a few command line arguments.
I usually call this main() from a bash script. Now, I would like to put everything into a small package, so I thought that maybe I could turn my simple bash script into a Python script and put it in the package.
So, how do I actually call the main() function of myModule.py from the main() function of MyFormerBashScript.py? Can I even do that? How do I pass any arguments to it?
For python main function, we have to define a function and then use if __name__ == '__main__' condition to execute this function. If the python source file is imported as module, python interpreter sets the __name__ value to module name, so the if condition will return false and main method will not be executed.
The main function in Python acts as the point of execution for any program. Defining the main function in Python programming is a necessity to start the execution of the program as it gets executed only when the program is run directly and not executed when imported as a module.
import __main__ Regardless of which module a Python program was started with, other modules running within that same program can import the top-level environment's scope (namespace) by importing the __main__ module.
When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.
It's just a function. Import it and call it:
import myModule myModule.main()
If you need to parse arguments, you have two options:
Parse them in main()
, but pass in sys.argv
as a parameter (all code below in the same module myModule
):
def main(args): # parse arguments using optparse or argparse or what have you if __name__ == '__main__': import sys main(sys.argv[1:])
Now you can import and call myModule.main(['arg1', 'arg2', 'arg3'])
from other another module.
Have main()
accept parameters that are already parsed (again all code in the myModule
module):
def main(foo, bar, baz='spam'): # run with already parsed arguments if __name__ == '__main__': import sys # parse sys.argv[1:] using optparse or argparse or what have you main(foovalue, barvalue, **dictofoptions)
and import and call myModule.main(foovalue, barvalue, baz='ham')
elsewhere and passing in python arguments as needed.
The trick here is to detect when your module is being used as a script; when you run a python file as the main script (python filename.py
) no import
statement is being used, so python calls that module "__main__"
. But if that same filename.py
code is treated as a module (import filename
), then python uses that as the module name instead. In both cases the variable __name__
is set, and testing against that tells you how your code was run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With