I want to run my program while my pc start

Viewed 77

This code can read a string and extract an output with vocal . The code run without problem.The only problem that i face is that i want to run the .py file everytime that i turn on my pc. I put also the .py file in the Startup Folder but nothing happened . This is my code:

from plyer import notification
import getpass
import os
import speech_recognition as sr
import playsound
from gtts import gTTS

USER_NAME = getpass.getuser()
notification.notify(
# title of the notification,
# the body of the notification
message="Welcome",
# the notification stays for 50sec
timeout=0.5,
           )

def speak(text):
   tts = gTTS(text=text, lang="en")
   filename = "voice.mp3"
   tts.save(filename)
   playsound.playsound(filename)

speak("Welcome Michael")
2 Answers

One Solution is a Batch file that calls the python code. The file in your startup will not do anything if nothing actually calls the file. in a .BAT file you need two things:

  1. The path to your python interpreter
  2. the path to your script

It would be then called by placing the batch file in the startup folder

"interpreter.exe" "your_script.py"

Solution 1:

You can put run_script.cmd in the Startup directory

C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Also, you need to add the Content of run_script.cmd Simply add this to the startup bat file in windows.

python path\to\your\script.py

Solution 2:

The following function will create a bat file in the startup folder that will run your script.

the file_path is the path to the file that you would like to run when your computer opens. you can leave it blank in order to add the running script to the startup.

import getpass
USER_NAME = getpass.getuser()


def add_to_startup(file_path=""):
    if file_path == "":
        file_path = os.path.dirname(os.path.realpath(__file__))
    bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
    with open(bat_path + '\\' + "open.bat", "w+") as bat_file:
        bat_file.write(r'start "" %s' % file_path)
Related