I have a file (test.py) that receives sys.argv from the console/bash:
import sys
def main():
ans = int(sys.argv[1])**int(sys.argv[1])
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
main()
Usually, I could just do $ python test.py 2 to produce the test.out file. But I need to call the main() function from test.py from another script.
I could do as below in (call.py) but is there any other way to run pass an argument to sys.argv to main() in `test.py?
import os
number = 2
os.system('python test.py '+str(number))
Please note that I CANNOT modify test.py and I also have a main() in call.py which does other things.
You can use your program as it is. Because, irrespective of the file invoked by python, all the python files will get the command line arguments passed.
But you can make the main function accept sys.argv as the default parameter. So, main will always take the sys.argv by default. When you pass a different list, it will take the first element and process it.
test.py
import sys
def main(args = sys.argv):
ans = int(args[1])**int(args[1])
with open('test.out', 'w') as fout:
fout.write(str(ans))
call.py
import sys, test
test.main()
Write that like:
import sys
def main(num):
ans = int(num)**int(num)
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
main(sys.argv[1])
so that your main() function doesn't have to know about sys.argv - it just handles the parameters being passed in to it.
Without modifying test.py you can still run it just as you have it, just do call.py:
import test
test.main()
then $ python call.py ARG will still work. Since you've already imported sys in test, you don't need to reimport it unless you want to use sys in call.py. Note that sys.argv[0]=='call.py' not test.py if use test through call.
Create a function to do your calculation and file writing, which can be called from any other module:
power.py:
import sys
def power(arg):
ans = arg ** arg
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
power(int(sys.argv[1]))
other.py:
import power
power.power(2)
Related
i would like to get at least one sys argument when running python script:
if i am not getting at least one argument program should print missing argument text and if i am passing to pyhton script at least 1 argument so run the function in loop
i am getting an error when i am trying to run it. can someone help me with that?
import os
import sys
from time import sleep
argv = sys.argv[1]
my_script = """ipconfig/all >>c://log//out.txt"""
def func():
os.system(my_script)
if len(sys.argv) <= 1:
print("missing argument")
else:
for i in range(int(sys.argv[1:])):
func()
sleep(2)
When i import one of my python scripts and run my current script , it seems to be running and displaying the output of the imported script which is really unusual behaviour. I have just imported this in my script but not really called any of its functions in my main code. How can i avoid from this behaviour happening ?
If i pass the -d flag with my main script it will run the usual code in my main script only
If i pass the -t flag with my main script , it will run the code from the imported python script only
main.py
import os
import argparse
import functions as funcs
import generate_json as gen_json
from test_compare_filesets import tester as imptd_tester
def get_json_location():
path = os.getcwd() + '/Testdata'
return path
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--export-date", action="store_true", required=True)
parser.add_argument("-t", "--execute-test", action="store_true", required=False)
args = parser.parse_args()
date = args.export_date
testt = args.execute_test
yml_directory = os.listdir('yaml/')
yml_directory.remove('export_config.yaml')
with open('dates/' + date + '.json', 'w') as start:
start.close()
for yml in yml_directory :
print("Running export for " + yml)
yml_file = os.path.join('yaml/' + yml)
json_path = get_json_location()
yml = funcs.read_config(yml_file)
data_folder = date
gen_json.generate_data_report(json_path , yml , data_folder)
if __name__ == '__main__':
main()
test_files.py
import generate_report as generate_reportt
def compare_filesets(file_names, previous_data, current_data):
for item in file_names:
print(item + generate_reportt.compare(previous_data.get(item), current_data.get(item)) + "\n")
def test_filesets():
'''
Test for scenario 1
'''
dict_1 = generate_reportt.read_file_into_dict("dates/2018-01-01.json")
dict_2 = generate_reportt.read_file_into_dict("dates/2018-01-02.json")
print(" Test 1 ")
compare_filesets(file_names=['a.json', 'b.json', 'c.json'],
previous_data=dict_1,
current_data=dict_2
)
This is why using the statement:
if __name__ == "__main__":
main()
is very important. You will want to add this to the script you're importing, and put all of your code that is being called within a main() function in that script. The variable __name__ of a script changes depending on whether the script is imported or not. If you're not importing the script and running it, then that script's __name__ variable will be "__main__". However, if it is imported, the __name__ variable turns into the script's filename, and therefore everything in main() function of that script will not be run.
For more information: What does if __name__ == "__main__": do?
consider I am having a following code in my bin as follows(filename: emp_dsb):
import sys
from employee_detail_collector.EmpCollector import main
if __name__ == '__main__':
sys.exit(main())
In my command line I will execute the "emp_dsb", so that above code will execute the main function from "employee_detail_collector.EmpCollector"
Code in (employee_detail_collector.EmpCollector) main():
def main():
try:
path = const.CONFIG_FILE
empdsb = EmpDashboard(path)
except SONKPIExceptions as e:
logger.error(e.message)
except Exception as e:
logger.error(e)
Now I need to add some argument here for emp_dsb, that is like "emp_dsb create_emp" should invoke a new set of functionalities for creating a employee, which is also needs to be added in same main()
someone look and let me know your ideas, If not clear let me know so that i will try to make it more clear.
the standard way to use command line arguments is to do this:
import sys
if __name__ == '__main__':
print(sys.argv)
read up on the doc of sys.argv.
then there are fancier ways like the built-in argparse and the 3rd party docopt or click.
I would personally use 'argparse' module.
Here is the link to a dead simple code sample.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)
I have two files: script1.py and script2.py. I need to invoke script2.py from script1.py and return the value from script2.py back to script1.py. But the catch is script1.py actually runs script2.py through os.
script1.py:
import os
print(os.system("script2.py 34"))
script2.py
import sys
def main():
x="Hello World"+str(sys.argv[1])
return x
if __name__ == "__main__":
x= main()
As you can see, I am able to get the value into script2, but not back to script1. How can I do that? NOTE: script2.py HAS to be called as if its a commandline execution. Thats why I am using os.
Ok, if I understand you correctly you want to:
pass an argument to another script
retrieve an output from another script to original caller
I'll recommend using subprocess module. Easiest way would be to use check_output() function.
Run command with arguments and return its output as a byte string.
Sample solution:
script1.py
import sys
import subprocess
s2_out = subprocess.check_output([sys.executable, "script2.py", "34"])
print s2_out
script2.py:
import sys
def main(arg):
print("Hello World"+arg)
if __name__ == "__main__":
main(sys.argv[1])
The recommended way to return a value from one python "script" to another is to import the script as a Python module and call the functions directly:
import another_module
value = another_module.get_value(34)
where another_module.py is:
#!/usr/bin/env python
def get_value(*args):
return "Hello World " + ":".join(map(str, args))
def main(argv):
print(get_value(*argv[1:]))
if __name__ == "__main__":
import sys
main(sys.argv)
You could both import another_module and run it as a script from the command-line. If you don't need to run it as a command-line script then you could remove main() function and if __name__ == "__main__" block.
See also, Call python script with input with in a python script using subprocess.
I am trying to call a script from python-daemon but its not working. this is what i am tying to do, is it correct?
I also want to pass a random argument to that script, currently i have hard coded it
import daemon
import time
import subprocess
import os
def interval_monitoring():
print "Inside interval monitoring"
while True:
print "its working"
# os.system("XYZ.py 5416ce0eac3d94693cf7dbd8") Tried this too but not working
subprocess.Popen("XYZ.py 5416ce0eac3d94693cf7dbd8", shell=False)
time.sleep(60)
print "condition true"
def run():
print daemon.__file__
with daemon.DaemonContext():
interval_monitoring()
if __name__ == "__main__":
run()
If you didn't make XYZ.py executable and added #!/usr/bin/env python in the top line, you need to call it via python, rather than directly. So your line would be something like this:
subprocess.check_output(["python", "XYZ.py", "5416ce0eac3d94693cf7dbd8"])