![]() |
autostart python scripts in background (Windows10) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: autostart python scripts in background (Windows10) (/thread-21442.html) |
autostart python scripts in background (Windows10) - john36 - Sep-30-2019 Hi, I have problem with autostart python scripts in Windows10: I made BAT file and put it to autostart folder, BAT file contens: @echo off "C:\Windows\py.exe" "C:\Users\OEM\Desktop\program\main_v4.py" pause Python script (main_v4.py) run another scripts: os.system('python connect_to_hortex_base_palette.py') but when i rebot win I recive error: python: can't open file 'connect_to_hortex_base_palette.py': [Errno 2] No such file or directory _________________________________________________________________________________________________ But when I Run main_v4.py by pressing file by click mouse scripts work well. as I saw when I click mouse the script, I see c:\windows\py.exe run the script, but when I rebot win I see c:\windows\system32\cmd.exe My request is: how i can run script correctly in background without windows(for example cmd window)? *I know pythonw.exe run scripts in backround but have the same problem as described above. RE: autostart python scripts in background (Windows10) - snippsat - Sep-30-2019 For this i would use schedule. Then just put the python file in startup folder. file associations to .py your main Python version eg Python 3.7.If problem with basic setup look here Python 3.6/3.7 and pip installation under Windows. So to do a test,a little annoying code if try to close notepad it will spawn a new one every 10-sec. If leave notepad open will do nothing,this make it easy to see that something is running in background at startup. This dos not start cmd if put in start up folder run silent in background,if running normal in cmd(better cmder) you will see output of print. # Install pip install schedule pip install psutil # start_notepad.py import schedule import time, os import psutil def check_process(): PROCNAME = "notepad.exe" for proc in psutil.process_iter(): if proc.name() == 'notepad.exe': return True else: return False def check_run(): if check_process(): print('Notepad is running') else: print('Notepad in not running') print('Start <notepad.exe>') os.startfile("notepad.exe") schedule.every(.10).minutes.do(check_run) while True: schedule.run_pending() time.sleep(1) RE: autostart python scripts in background (Windows10) - john36 - Sep-30-2019 (Sep-30-2019, 03:21 PM)john36 Wrote: os.system('python connect_to_hortex_base_palette.py')I added C:/Users/OEM/Desktop/program/: os.system('python C:/Users/OEM/Desktop/program/connect_to_hortex_base_palette.py')and now it's working but with popup window. Now I'm still fighting with pythonw.exe (without windows), but now I will try you adwise, thank You RE: autostart python scripts in background (Windows10) - snippsat - Sep-30-2019 Do not use os.system() ,subprocess has replaced it in safer way a long time ago.Can force silent always using subprocess. import subprocess si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW #si.wShowWindow = subprocess.SW_HIDE # default subprocess.run(['python', 'start_notepad.py'], startupinfo=si) RE: autostart python scripts in background (Windows10) - john36 - Oct-01-2019 (Sep-30-2019, 11:18 PM)snippsat Wrote: Do not useIt's working, Thank You. If I can ask, how can I run 5 scripts with this module(subprocess)? Can I use subrocess without output return from subprocess? All my python scripts works in infinity loops. |