In Python, can I call the main() of an imported module?
Asked Answered
B

7

136

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?

Bly answered 24/1, 2013 at 11:23 Comment(5)
If you have imported myModule, then you should be able to call myModule.main(). What have you tried so far?Viens
I'm worried by the input arguments, which I usually pass from a shell script.Bly
Does it make sense to call it with the subprocess module?Jacki
I guess it would be easier, yeah.Bly
The "main" function is just a function, there is NOTHING special about it. It's just convention, at this point, to name this function "main".Norvell
C
161

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.

Cathouse answered 24/1, 2013 at 11:26 Comment(4)
Sure. But what about the input arguments? I use argparse, so when I call the script from a terminal, I do $ python myModule -a input_a -b input_b --parameterC input_c. How would it work from within python code? That's what I couldn't find from a simple search.Bly
@RickyRobinson: expanded to show that you can have it both ways; just pass in the arguments parsed or to-be-parsed.Cathouse
Thanks. Could you please also specify which excerpt of code belong to which module or script? It looks way more cumbersome than what I thought at the beginning.Bly
@RickyRobinson: Both excerpts belong together in the same module; I've made that explicit.Cathouse
Y
60

Martijen's answer makes sense, but it was missing something crucial that may seem obvious to others but was hard for me to figure out.

In the version where you use argparse, you need to have this line in the main body.

args = parser.parse_args(args)

Normally when you are using argparse just in a script you just write

args = parser.parse_args()

and parse_args find the arguments from the command line. But in this case the main function does not have access to the command line arguments, so you have to tell argparse what the arguments are.

Here is an example

import argparse
import sys

def x(x_center, y_center):
    print "X center:", x_center
    print "Y center:", y_center

def main(args):
    parser = argparse.ArgumentParser(description="Do something.")
    parser.add_argument("-x", "--xcenter", type=float, default= 2, required=False)
    parser.add_argument("-y", "--ycenter", type=float, default= 4, required=False)
    args = parser.parse_args(args)
    x(args.xcenter, args.ycenter)

if __name__ == '__main__':
    main(sys.argv[1:])

Assuming you named this mytest.py To run it you can either do any of these from the command line

python ./mytest.py -x 8
python ./mytest.py -x 8 -y 2
python ./mytest.py 

which returns respectively

X center: 8.0
Y center: 4

or

X center: 8.0
Y center: 2.0

or

X center: 2
Y center: 4

Or if you want to run from another python script you can do

import mytest
mytest.main(["-x","7","-y","6"]) 

which returns

X center: 7.0
Y center: 6.0
Younglove answered 10/5, 2016 at 17:14 Comment(3)
This is exactly what I needed - thank you so much for the helpful addendumDinnie
This might have worked with Python 2, but in Python 3 it doesn't work anymore, can't call main() function of a module: AttributeError: module 'sqlacodegen' has no attribute 'main'Waxbill
@Waxbill that's due to how sqlacodegen is written. It's main is in sqlacodegen.cli and is written to only parse args from sys.argv[1:]. You cannot provide args to main.Albuquerque
P
39

It depends. If the main code is protected by an if as in:

if __name__ == '__main__':
    ...main code...

then no, you can't make Python execute that because you can't influence the automatic variable __name__.

But when all the code is in a function, then might be able to. Try

import myModule

myModule.main()

This works even when the module protects itself with a __all__.

from myModule import * might not make main visible to you, so you really need to import the module itself.

Percussive answered 24/1, 2013 at 11:30 Comment(4)
Oh ok, thanks for the clarification. I put everything in a main() function, so it should be ok. I'm more concerned about how to pass input arguments to this "second" main. Any easy way to do so?Bly
Sure: import sys; module.main(sys.argv);Percussive
This is a great alternate explanation about accessing __main__ which helped me, thanks @AaronDigullaExpendable
Here is a trick to call a main from another function: https://mcmap.net/q/12868/-what-does-if-__name__-quot-__main__-quot-doOrianna
P
6

The answer I was searching for was answered here: How to use python argparse with args other than sys.argv?

If main.py and parse_args() is written in this way, then the parsing can be done nicely

# main.py
import argparse
def parse_args():
    parser = argparse.ArgumentParser(description="")
    parser.add_argument('--input', default='my_input.txt')
    return parser

def main(args):
    print(args.input)

if __name__ == "__main__":
    parser = parse_args()
    args = parser.parse_args()
    main(args)

Then you can call main() and parse arguments with parser.parse_args(['--input', 'foobar.txt']) to it in another python script:

# temp.py
from main import main, parse_args
parser = parse_args()
args = parser.parse_args([]) # note the square bracket
# to overwrite default, use parser.parse_args(['--input', 'foobar.txt'])
print(args) # Namespace(input='my_input.txt')
main(args)
Patriotism answered 1/12, 2017 at 3:17 Comment(0)
S
4

I had the same need using argparse too. The thing is parse_args function of an argparse.ArgumentParser object instance implicitly takes its arguments by default from sys.args. The work around, following Martijn line, consists of making that explicit, so you can change the arguments you pass to parse_args as desire.

def main(args):
    # some stuff
    parser = argparse.ArgumentParser()
    # some other stuff
    parsed_args = parser.parse_args(args)
    # more stuff with the args

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

The key point is passing args to parse_args function. Later, to use the main, you just do as Martijn tell.

Sueannsuede answered 3/12, 2014 at 11:53 Comment(0)
S
0

Assuming you are trying to pass the command line arguments as well.

import sys
import myModule


def main():
    # this will just pass all of the system arguments as is
    myModule.main(*sys.argv)

    # all the argv but the script name
    myModule.main(*sys.argv[1:])
Selfexpression answered 24/1, 2013 at 11:31 Comment(2)
Thanks. I am actually using argparse instead of sys.argv. How would it change in this case? Also, from the outer script I just want to pass a few input arguments the user types in, while other input arguments for the inner script (myModule.py) are hardcoded by me.Bly
Without seeing the code itself it's hard to answer specifics. In general, you would just pass whatever arguments. The * unpacks an array f(a) => f([1,2,3]) vs f(*a) => f(1,2,3) You could just as easily do myModule.main(sys.argv[1], "other value", 39) or whatever.Selfexpression
A
0

I hit this problem and I couldn't call a files Main() method because it was decorated with these click options, eg:

# @click.command()
# @click.option('--username', '-u', help="Username to use for authentication.")

When I removed these decorations/attributes I could call the Main() method successfully from another file.

from PyFileNameInSameDirectory import main as task
task()
Amora answered 31/5, 2021 at 4:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.