Python Forum

Full Version: autostart python scripts in background (Windows10)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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)
(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
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)
(Sep-30-2019, 11:18 PM)snippsat Wrote: [ -> ]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)
It'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.