This question already has answers here:
Running bash script from within python
(7 answers)
Closed 5 years ago.
I am trying to run bash script.sh file in Python.
I tried running it through following code:
subprocess.call("script.sh", shell=True)
but it runs the script outside python and asks Windows application to run this file.
I want this script.sh to be run in Python command line.
I want Python to run this script.
Import the library OS and then use os.system:
import os
os.system('bash')
Related
This question already has answers here:
How to execute Python inline from a bash shell
(4 answers)
Closed 2 years ago.
In a shell, I can execute AppleScript command-line code like so:
osascript -e "tell application \"Finder\" to activate"
Is the same thing possible in Python, e.g.:
python --execute "print('hello world!')"
Something like this should work for you!
python -c 'print("Hi")'
Probably a duplicate of How to execute Python inline from a bash shell
This question already has answers here:
Run multiple python scripts concurrently
(10 answers)
Closed 4 years ago.
I was wondering if there is a way to run two python scripts at the same time through the command prompt.
I also am wondering if there is a way to run a second python script after another one has already been executed and is currently running.
Thanks
To execute 2 script at the same time, execute python script1.py & python script2.py
To execute one after the other (without waiting for the first one to finish), you could simply open 2 command prompt windows. Execute each script in each window.
To execute one after the other (after waiting for the first one to finish successfully), execute python script1.py && python script2.py
This question already has answers here:
Shell Script: Execute a python program from within a shell script
(11 answers)
Closed 5 years ago.
I know how to program a python, but I have no idea about Unix, so how can I create two .sh files to compile and execute my python program. For example, my program named hello.py and I have two files named compile.sh and execute.sh, I want to invoke compile.sh then hello.py will be compiled, and invoke execute.sh then hello.py will be executed. Thanks!
Instead of writing Shell scripts (the .sh files) you can just open a terminal and do stuff from there, see http://www.dummies.com/computers/macs/mac-operating-systems/how-to-use-basic-unix-commands-to-work-in-terminal-on-your-mac/ - whatever you do there is executed live as if it was a Shell script.
By opening a terminal and simply typing
python my_script.py
you trigger python to first compile the code to *.pyc files and then execute it. No special steps are needed! Whatever you type in the terminal, you can also save it as a shell script and run it at a later point. Shell scripting is taught at many websites like https://www.shellscript.sh/ and others, google is your friend.
This question already has answers here:
Get program execution time in the shell
(11 answers)
Closed 5 years ago.
I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.
Any leads would be helpful thanks in advance.
You can use the time command to get the runtime of the python script.
]$ cat time_test.bash
#!/bin/bash
# You can use: time python script.py
time python -c 'import os;os.getenv("HOME")'
Output will be something like this
]$ ./time_test.bash
real 0m0.010s
user 0m0.005s
sys 0m0.005s
Call the python script with /usr/bin/time script. This allows you to track CPU and wall-clock time of the script.
This question already has answers here:
Find full path of the Python interpreter?
(3 answers)
Closed 6 years ago.
I would like to output, in my script, the full path of the Python interpreter running it:
#!/usr/bin/env python
print("{}".format(full_path_of_interpreter_running_this_script)
The script is in the PATH and run as:
script.py
Can I do that? How?
Note: Doing which python or type python in bash does not help me, because I am using pyenv, and pyenv is doing shims magic.
Note: More than identifying the Python executable, I am interested in identifying the virtualenv that is being used, and I thought knowing the full path to the interpreter will help me in this.
This gives the full path to the command that was used to run the script:
import sys
print(sys.executable)