0

I am using python 3.5

I want to run a python script calling from another python script.

In particular, say I have script A (in particular, this is the exact file that I want to run: script A file):

Script A is the following.

if __name__ == '__main__': args = argparser.parse_args() _main(args) 

I am running script B, inside script B, it calls script A. How do I simply do this by calling the main function of script A while running script B?


Please no os.system('python scriptA.py 1'), this is not what i want. thanks

4 Answers 4

2

normally you can import it and call the main function like

import script_a ... script_a._main() 

of course it could be that the script_a is not in you src structure, so that you can not simple import, or script_a is completely somewhere else. Suppose the path of the script_a is path_a you can

import sys sys.path.append(path_a) import script_a script_a._main() 

if you want to pass the args to script_a , in your script_b

import script_a ... if __name__ == '__main__': args = argparser.parse_args() script_a._main(args) 
Sign up to request clarification or add additional context in comments.

4 Comments

but it has args = argparser.parse_args()
@wrek as you would prepare those arguments somehow, there is no problem with passing them to _main directly.
I thought I have to do args = script_a.argparser.parse_args(). Then script_a._main(args)
I can simply do script_a._main() in one shot?
1

In Script B simply import Script A,

import script_A 

or

from script_A import * 

now you can access your script A in script B

2 Comments

how do I call that main thing
if you import * you can access all function which is present in script a
1

Treat the file like a module and put import scriptA at the top of your file.

You can then use scriptA.main(1) where 1 is the argument you are passing to it.

N.B When importing do not put .py at the end.

5 Comments

why do i have a 1, I dont need to pass anything
You literally have os.system('python scriptA.py 1') in your question .... your example is passing 1 LMAO also all the other answers changed the name of your script..
I thought I have to do args = script_a.argparser.parse_args(). Then script_a._main(args)
I can simply do script_a.main() in one shot?
@wrek try it and see what happens? :)
0

If you have code which is not indented inside of script A and if you import script A inside script B, it will automatically first run script A and then move on to the __main__() of script B. How ever if you wish control when the execution of script A begins, then , indent your code in Script A or code it in a normal function such as def start() .

Now, import Script A into Script B as follows

import ScriptA 

And run the script A as

ScriptA.start() 

NOTE: Make sure that script A and script B are in the same directory for this to work. Hope this solves your purpose. Cheers!

2 Comments

The things you describe (automatically started __main__() and postponed execution of imported scripts) do not exist.
You are right. I got confused . Now it should be alright

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.