Execute File from python [duplicate] - python

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 7 years ago.
Im trying to make a python program, and i just can't find any information on Google on how to just execute a non-python file from python (with an if statement). For Example:
if abc == "abc":
"Execute ThisFile.sh"
EDIT: this was marked as dupe, but I am not trying to run a single command, I want to execute the entire file.

Use subprocess module.
if abc == 'abc':
subprocess.check_output('bash file.sh', shell=True)

Related

how to set an environment variable value while calling a python script using bash in a single command [duplicate]

This question already has answers here:
What is the difference between an inline variable assignment and a regular one in Bash?
(2 answers)
Closed 2 years ago.
I would like to execute a python file app.py like below:
python app.y
But I would like to pass to this file a new value = 'ok' for the env variable called 'training'.
How can I do this from shell with a single command?
I think the approach should be a little different:
set an environment variable: $ export training=ok
inside your Python script, read the environment variable:
import os
print(os.getenv('training'))
Another option would be to use parameters and parse them with something like argparse.

How to terminate python program with given signal? [duplicate]

This question already has answers here:
Exit codes in Python
(14 answers)
Closed 4 years ago.
In C, I'd do something like
int main() {
return 42;
}
and after executing the program, I can(in Linux) prompt echo $? to the console to get the desired 42 as the return signal from the last operation. How can I do that in python?
I tried just creating a file with a return 42 in it, but it says that it is not inside a function(obviously).
exit(42)
will give you the desired behaviour.

Is there a non-pdb way to enter Python interpreter at a given line? [duplicate]

This question already has answers here:
drop into python interpreter while executing function
(3 answers)
Closed 5 years ago.
Is there a way to cause a python script to enter the interpreter at a given line? I know I can use import pdb; pdb.set_trace() to begin debugging at a certain line, but that means all the pdb commands may get in the way.
Is there a way to kick into the regular interpreter (or ipython) outside of pdb?
The easiest way is code.interact:
import code
code.interact(local={**globals(), **locals()})

Python: use main function [duplicate]

This question already has answers here:
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 6 years ago.
I have code with many functions and main, when I am trying to run the code it's not working and showing like it run successfully. When I am run the debugger it's show me that it run only on the functions' names. so I am pretty sure the problem it's with the main. how can i solve it?
main() is not run implicitly (like in C or Java). In Python you have to explicitly call if you want your code to run.
def main():
some_code()
if __name__ == "__main__":
main() # actually run main
Note that main does not have to be named main - it may be arbitrary named function. Moreover, code to run does not even have to be enclosed in any function. Consider file with content like that:
print "abc"
It'll simply print "abc" on standard output.

How to open a python script with a constructor variable [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
So i've been at this one for a little while and cant seem to get it. Im trying to execute a python script via terminal and want to pass a string value with it. That way, when the script starts, it can check that value and act accordingly. Like this:
sudo python myscript.py mystring
How can i go about doing this. I know there's a way to start and stop a script using bash, but thats not really what im looking for. Any and all help accepted!
Try the following inside ur script:
import sys
arg1 = str(sys.argv[1])
print(arg1)
Since you are passing a string, you need to pass it in quotes:
sudo python myscript.py 'mystring'
Also, you shouldn't have to run it with sudo.

Categories

Resources