I'm trying to run one script from another script. I've read; What is the best way to call a script from another script? and I can't seem to get this to work.
My main script (Script A) does a lot of image processing and GUI interactions. However, randomly an error message or other window might appear interrupting the GUI interactions until the message or window is closed.
I've written a second script (Script B) that I want to run perpetually that closes these windows or error messages when discovered.
I'm trying to call Script B from Script A like this:
import close_windows
close_windows.closeWindows
print("Starting Close Windows....")
And Script B is:
import pyautogui as py
def closeWindows():
image = r'C:\image.jpg'
image2 = r'C:\image2.jpg'
while True:
foundimage = py.locateCenterOnScreen(image)
foundimage2 = py.locateCenterOnScreen(image2)
if foundimage or foundimage2 != None:
py.click(1887, 65)
When I run script B independently it works, when I try running it via Script A with close_windows.closeWindows nothing happens.
I've also tried from close_windows import closeWindows and calling closeWindows but again, nothing happens.
Related
How to run a python program in between another python program
os.system(r'pythonw.exe D:\text.py')
I have tried this but it had pause execution between the main program
and also tried
subprocess.run(r'pythonw.exe D:\text.py')
suppose I have program text.py which notify me every hour
I have main program i.e.
code...
os.system(r'pythonw.exe D:\text.py')
code...
print("hello world")
my command should run text.py in the background and execute the following code.
You can use Python Library named "Soldier". This is very easy to use and implement.
https://pypi.org/project/soldier
import soldier
firefox_process = soldier.run('firefox', background=True)
firefox_process.pid
I have a python script calling another program with the os.system command. It is a very complex program which can be called in the Terminal using ./myProgram. I want to automatically execute said program and do different stuff (which works fine) in between.
Somehow this works:
print('start')
os.system('ll')
print('end')
But calling the program with the python script:
print('start')
os.system('./myProgram')
print('end')
just executes myProgram without showing the print statements. myProgram itself displays information in the terminal.
Later I want to do more than just print something in between.
I tried using the subprocess module:
print('start')
subprocess.call('./myProgram', shell = True)
print('end')
which shows the same results as the os.system module.
Which properties of a program do not allow my python script to run properly?
And how can I call another program with my python script, execute said program and continue with the script afterwards?
u can always use a safe and secure way to call it
if u want to call app.py in the folder scripts_folder
just write:
from scripts_folder import app
Now if u have a def inside app.py for example my_def_example() u can call it by writing:
app.my_def_example()
all.
Is there a way, using Python, to check if the script that is currently running is requested to close? For example, If I press the X-Button (close program button) on the top-right to close it, or end the script in any other way, can the script do some code before it ends? Example:
# script goes here...
if Script_To_Be_Closed: # replace this with an actual line of code.
do_stuff
There are multiple options you may use, like trapping keyboardinterrupts, but the simplest is atexit, which executes a function whenever a scripts is ended (except of a hard process kill indeed).
import atexit
def my_exit_function(some_argument):
// Your exit code goes here
print(some_argument)
if __name__ == '__main__':
atexit.register(my_exit_function, 'some argument', )
// Your script goes here
You can use a shell script to do the job
You can see the script command shown below which calls itself after executing the command to run the python file. once the python file is closed the next line will force the python command to run again. you can also customise the behaviour the way you want.
main.py
#!/bin/bash
python3 ./main.py
source ./infiniteRun.sh
If you need to stop the job just edit the file and remove the last line source ./infiniteRun.sh and save the file.
I want to start a python script and then automatically close that script after 2 minutes, run another command, and keep doing the same thing again like this (loop) forever :
Cd c:/location.of.script/
pythonscript.py
Stop (like ctrl+c) pythonscript.py after 120s
Del -f cookies.file
.
.
.
Is this even possible with a batch file on windows 10? If so, can someone please help me with this?
I’ve been looking everywhere but found nothing except the exit() command which stops the script from inside - this isn’t what I want to do.
You can change your python script to exit after 2 minutes, and you could batch file that has a while loop that runs forever and run the python script then deletes the cookie.file, I don't know if that's exactly what you want, but you can do it by putting a timer in your python script.
You can make a separate thread that keeps track of the time and terminates the code after some time.
An example of such a code could be:
import threading
def eternity(): # your method goes here
while True:
pass
t=threading.Thread(target=eternity) # create a thread running your function
t.start() # let it run using start (not run!)
t.join(3) # join it, with your timeout in seconds
And this code is copied from https://stackoverflow.com/a/30186772/4561068
I have a python script running like this on my server:
python script.py &
The script works fine, but constantly I'm adding new things to the script and re-running it, somedays it runs for days without any problem, but sometimes the script stops running (Not running out of memory), but since I started the script as background I have no idea how to check for the Exception or error that cause the script to stop running. I'm on a Ubuntu server box running in Amazon. Any advice on how to approach this inconvenience ?
I use something like this. It will dump the exception which caused termination to your syslog, which you can see by examining /var/log/syslog after your script has stopped.
import traceback
import syslog
def syslog_trace(trace):
'''Log a python stack trace to syslog'''
log_lines = trace.split('\n')
for line in log_lines:
if len(line):
syslog.syslog(line)
def main():
# Your actual program here
if __name__ == '__main__':
try:
main()
except:
syslog_trace(traceback.format_exc())