Running Python Scripts in Background as Windows Process & Converting Python Scripts Into Windows Executables.
The main purpose of this article is to demonstrate the following points with the help of a simple drink water reminder code:
- Sending Windows Notification with Python
- Running Python Script as Windows Process in the background (Without Console/CMD)
- Converting Python Script into Windows Executable (.exe) file
Sending Drink Water Notification in Python
win10toast is the Python library for sending windows notification from Python code. Its pretty simple to use & a straight-forward library. We will use this library to send Drink Water notification. We will use the time library to repeat this notification after a certain period.
The above code is pretty self-explanatory. We are setting the reminder_duration, which is the number of seconds after which the notification should be sent. After that, we create an instance of ToastNotifier which we will use to send windows notification.
In the While loop, we are setting windows notification (i.e. the message we want to display in the notification). Now, we set it to sleep for the assigned duration of time before it would send notification again.
Output
Running Python script as a background Windows Process
Now, the most common way to run the python script is by typing Python Script.py in Console. The problem with this is that we will have to keep the console/cmd open to keep the script running. If we talk about our Drink Water reminder script, then we will have to keep the console for it to keep it running and sending periodic notifications.
To keep python script running in the background is to save the script as .pyw format. The .pyw extension will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on the execution of the code.
All you have to do now is double click the file and it will execute as a windows background process. It will keep on running in the background until you end the python process from task manager.
Converting Python Script into Windows Executable File
To execute the same python code into a different windows system, you will have to install Python into that system. Not only that but also you will have to install the dependent libraries. To overcome this problem, we convert our Python script into .exe file so that it can easily run on other windows system without having to install Python or its libraries.
For this, we will be using a python library called pyinstaller. You can install it using pip command. Now, we will open cmd in the directory where our Python script (DrinkWater.py) is located. We will write the following command.
pyinstaller --onefile -w filename.py ______________________________________ --onefile will create a one-file bundled executable -w On Windows this option will be set if the first script is a ‘.pyw’ file.
This command will take some time to execute depending on the size of the project. It will create a standalone multi-platform package which you can execute to run your script.
If successfully executed, you will see the following message in the cmd.
You will see the following folders in your directory. You can find .exe file in the dist folder. Consider all these folders as a package, you can move these folders to a new windows system and run the script standalone.
Head - Product Development
2yConverting Python Script into Windows Executable File how to go further and make this executable background windows process on another system without any dependencies.