I have a python file name "a.py" and another python file name "b.py". I wanted to run a.py with command in b.py file and I use this codes below:
import os
def b():
os.system("python a.py")
but a.py has a list name "list_exam" that I want to pass it to b.py through the command line code. I don't know how to pass it in a.py and how to get with command in b.py file.
thanks a lot.
-
Do you absolutely need to do this from the command line? importing would probably be much easier for this use caseFlyingTeller– FlyingTeller2018年08月02日 07:59:20 +00:00Commented Aug 2, 2018 at 7:59
-
in fact, my main command is os.system("python b.py -c configs/conf.json") and I want to send different config each time. @FlyingTellerMMRA– MMRA2018年08月02日 08:00:58 +00:00Commented Aug 2, 2018 at 8:00
-
serialize/de-serialize? or just use modules... if you have json you can pass more list data in thereJean-François Fabre– Jean-François Fabre ♦2018年08月02日 08:01:00 +00:00Commented Aug 2, 2018 at 8:01
-
Possible duplicate of What is the best way to call a Python script from another Python script?Cezar Cobuz– Cezar Cobuz2018年08月02日 08:04:19 +00:00Commented Aug 2, 2018 at 8:04
2 Answers 2
Please don't use shell commands to invoke other Python scripts, it is neither efficient nor scalable.
if a.py:
def do_somthing(param1, param2):
...
list_exam = make_result()
return list_exam
def main(config):
do_something(config['param1'], config['param2'])
you can write b.py like this:
import a
def b():
list_exam = a.do_something(config['param1'], config['param2'])
don't forget to create a __init__.py file in that dir.
Comments
You may want to use sys.argv to get command line parameters. Take a look at this: https://www.pythonforbeginners.com/system/python-sys-argv
But, maybe your question is already answered here: Passing a List to Python From Command Line