How to run Python scripts as a Windows service

Hello everybody! It’s been a while since my last posting, so please have mercy with my rusty writing skills.😊 In this tutorial, I’ll show you how to run Python applications and scripts as a Windows service and the reasons behind it. Let’s dive right into it.
TL;DR
“Why would you even…?”
If those words came into your mind while reading the title, let me stop you right there — you probably don’t really know what a Windows service is. Here’s a quick description.
A Windows service is basically a long-running application in your system. It can be configured to start automatically when you system is booting and also be paused, resumed or started manually.
The most common reason for running a Python script as a service would be to ensure an application runs and starts automatically when you login to your system. For example, you would use this if you have a web application that you want to be running all the time. A Windows service is a great solution for serving Flask or Django applications.
If you want to learn more about Windows services, I’d recommend you to read this.
Setup
Note: It might be ridicuolous to even state this, but Python needs to be pre-installed to run Python scripts.
Download NSSM
You can’t create a Windows service running a Python script by default, but there’s a great tool to solve this problem: NSSM. Simply download the tool and extract it. An installation isn’t required, all you need to do is to add it as a %PATH% environment variable to your system.
Important: Run the following commands as admin!
- Create a new service
nssm install “SERVICE_NAME” “PATH_TO_PYTHON.exe” “PATH_TO_SCRIPT.py”If you’re a lazy person like me, searching the Python path would be too much effort. Run this instead:
python -c “exec(\”import sys\nprint(sys.executable)\”)”The service is installed now and will start automatically every time your computer starts. If you were to open up the Windows Service GUI now, you’d see your service.
2. Add service parameters
The service might already work, but to be safe, log files for the standard output and error output should be configured:
nssm set SERVICE_NAME AppStderr PATH_TO_ERROR_FILE.log
nssm set SERVICE_NAME AppStdout PATH_TO_LOG_FILE.logImportant: The files and paths MUST already exist!
As an alternative, you can open up the GUI with the command
nssm edit SERVICE_NAMEConfigure it like shown below.

3. Start & Test the service
The configuration is complete now. The service can be started now.
nssm start SERVICE_NAMENote: In case this doesn’t work, try to start it manually in the Windows Services GUI (search for SERVICE_NAME → Start. This should work.)
Some more helpful commands:
nssm stop SERVICE_NAME //stops the service
nssm restart SERVICE_NAME //restarts the service
nssm remove SERVICE_NAME //delete the serviceThat’s it!🥳 Congratulations, you just learned how to run a Python script in a Windows service using NSSM.
Feel free to share this article if you liked it. Thank you for reading!






