avatarBetter Everything

Summary

The web content provides a comprehensive guide on how to run Python scripts automatically upon starting a Windows computer, detailing methods such as using the Startup folder, .cmd files, and Python scripts with the .pyw extension to execute scripts with or without a terminal window.

Abstract

The article "Run Python Scripts On Windows Startup" offers valuable insights for Python developers looking to automate the execution of their scripts at Windows startup. It outlines several approaches, including placing a Python script directly into the Startup folder, using a .cmd file to run scripts with command-line arguments, and employing a .pyw file to start multiple Python scripts without opening a terminal window. The article emphasizes the use of absolute file paths to ensure script functionality and discusses the advantages of using virtual environments for project-specific Python versions. Additionally, it provides tips for preventing terminal windows from appearing and for running scripts simultaneously, enhancing the user experience and maintaining a clean desktop environment upon system startup.

Opinions

  • The author suggests that using the .pyw extension is preferable to .py when placing scripts in the Startup folder to avoid opening a terminal window, indicating a preference for a cleaner user interface.
  • The use of .cmd files is recommended for passing command-line arguments to Python scripts, highlighting the method's flexibility and control over script execution.
  • The article implies that using absolute file paths is a best practice to avoid issues with relative paths when scripts are moved to the Startup folder.
  • The author expresses the utility of virtual environments, especially when managing multiple Python projects with different dependencies and versions.
  • The preference for using the subprocess module's Popen function to run multiple scripts simultaneously suggests the author values efficiency and parallel execution in script management.
  • The author's repeated emphasis on tips and best practices indicates a commitment to providing a user-friendly and robust solution for running Python scripts at Windows startup.

Run Python Scripts On Windows Startup

Learn to make Python scripts run automatically when starting a Windows computer!

Learn to make Python scripts run automatically when starting a Windows computer! Image by catalyststuff on Freepik

Have you ever made a Python script that needs to run each time a computer is started? Or a script that needs to be running all the time, like a directory watcher or a good habit reminder?

Then wouldn’t it be nice if those scripts would start automatically right when you start up your computer?

In this article we will take a look at how to make Python scripts run when starting up a Windows computer.

This article covers different use cases and consists of these parts:

  • Starting a Python file on Windows startup
  • Starting a Python file with a .cmd file on Windows startup
  • Starting Python files from a Python file on Windows startup

Starting a Python file on Windows startup

On Windows systems there exists a folder where you can place scripts to run when the computer starts.

You can go to the folder by pressing the Windows key (⊞) + R.

Then a program called Run appears in which you type shell:startup and hit enter. This will open the Startup folder.

You can then drag and drop or copy the Python file into the Startup folder.

Now, when you start the computer, the Python file will run automatically.

Tip 1: Using the .py extension for your Python file will make it so that a terminal window opens up as well. To prevent that you can use the .pyw extension.

Tip 2: If you use relative filepaths and relocate the file to the Startup folder your script might not work anymore. To fix that you can use absolute filepaths.

Starting a Python file with a .cmd file on Windows startup

To be able to pass along command line arguments when running your script on startup you can use a file with the .cmd extension.

For example you can use Notepad to make a file and save it as start_Python_file.cmd.

Inside that file you can write a command to start a Python file and add the command line arguments to it.

Here is an example of a command that runs a Python file named test.py and passes 2 arguments to it Argument1 and Argument 2:

py "C:\Users\BE\Desktop\test.py" argument1 "argument 2"

When you put a .cmd file with a line like that in the same Startup folder as we did before with the Python file, it will run and start the Python file when starting Windows.

Tip 3: The Python file itself does not need to be placed in the Startup folder in this case. As that would also make the Python file run once without command line arguments.

You can read this article to learn how to get the command line arguments as input in your Python file:

You can also specify a specific version of Python to use when running the script from a .cmd file. This is useful when you have a Python project inside a virtual environment for example.

Here is an example of a line inside a .cmd file that starts a Python file inside a virtual environment:

"C:\Users\BE\Documents\Project Maker\venv\Scripts\python.exe" "C:\Users\BE\Documents\Project Maker\main.py"

Tip 4: put quotes (") around arguments when they contain spaces.

You can read this guide to learn more about when and how to use virtual environments in Python:

Starting Python files from a Python file on Windows startup

As you might have noticed using .cmd or .py files works to get Python scripts running when starting up a Windows computer. However, they also open up a terminal window.

In this final section we will explore an approach to prevent that from happening. Which is by making a .pyw Python file and putting that in the Startup folder like we did before. But inside this Python file we then run commands to start other Python files.

Such commands can be made with the subprocess module from Python. To use that module, just add this line to your Python script:

import subprocess

The commands can contain command line arguments and specific versions of Python including those in virtual environments.

You can also start multiple Python files from one file by adding multiple commands.

The commands can be run with the run function like this:

import subprocess

command = 'py C:/Users/BE/Desktop/test.py'
subprocess.Popen(command, shell=True)

Or with the Popen function like this:

import subprocess

command = 'py C:/Users/BE/Desktop/test.py'
subprocess.Popen(command, shell=True)

That second approach makes it possible to run multiple files simultaneously instead of having to wait for one file to finish before starting the next.

Here is an example of running 2 files at the same time:

import subprocess

command = '"C:/Users/BE/Documents/Project Maker/venv/Scripts/python.exe" "C:/Users/BE/Documents/Project Maker/main.py"'
subprocess.Popen(command, shell=True)

command = '"C:/Users/BE/Documents/Project Maker/Projects/Database Creator/venv/Scripts/python.exe" "C:/Users/BE/Documents/Project Maker/Projects/Database Creator/main.py"'
subprocess.Popen(command, shell=True)

The code above can be put in a .pyw file and that file can be put inside the Startup folder. The file will then run when Windows starts and run both Python programs simultaneously.

Tip 5: Use forward slashes (/)instead of backslashes when working with filepaths in Python.

Thank you for reading!

I hope my post was helpful to you!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

Python
Programming
Automation
Technology
Windows
Recommended from ReadMedium