How to Run Python Scripts in the Background
There are two type of system scripts.
-
Interactive Scripts: These are scripts that we run with a program or terminal window open. For these scripts, we often want detailed output so we can closely monitor what the script is achieving and ensure it encounters no obstacles.
-
Background Scripts: These are silent partners, running in the background and performing minor tasks quietly, such as periodic data downloading, database updating, headline sniffing, file maintenance, etc.
For the latter, having a window open the entire time for something that is just supposed to work quietly is a waste of visual space. We want it to be as detached and absent as possible.
Below, I will show a simple way to set up just that. The example will be a Python script run through Task Scheduler.
Basics
To run scripts “silently”, we need a few things things:
- Python
Download the latest version from the official website.
- Virtual Environment
Although it’s not required, using a virtual environment is a very important habit. Virtual environments are self-contained environments that have a specific version of Python and a specific set of modules installed, tailored for one particular job.
If we install all the modules to the default system version of Python, the module list might grow exponentially, slowing down Python software execution. Additionally, we might encounter compatibility issues. If something happens to the core system Python, it could cause a system-wide meltdown.
To maintain order, simplicity, ease of access, and stability, it’s best to install a virtual environment for every different task or group of tasks our programs will be performing.
venv, a virtual library for Python, comes with Python itself, so there is no need to install it separately.
- Open a terminal (cmd or PowerShell) and navigate to the directory where you want to store your virtual environments. Choose a location that is convenient and logical.
- In the terminal, run: python -m venv followed by the name you want to give to the virtual environment. For example: python -m venv mybackup.
- To use the virtual environment, you need to activate it. When you create an environment using venv, a folder with that name is created, containing a Scripts folder, which contains an activate file. To start the script, run the command: source path/to/venv/myvenvname/Scripts/activate where myvenvname is the name of your virtual environment. Alternatively, you can navigate to the venv’s Scripts folder and run source activate.
- Once the virtual environment is activated, install all the modules required by the script you want to run in Task Scheduler. Open your script file(s) and review all the imports. Some of them are built-in (installed with Python), while others need to be downloaded externally. Download the external modules with the command pip install followed by the module name.
Once you are finished, you can exit the virtual environment by typing exit in the console window. Don’t worry - once everything is set up, you won’t have to manually enter the virtual environment for Task Scheduler to execute it.
- .pyw Python file
Python files can be saved with two file extensions: .py and .pyw. The latter, ‘w’ stands for ‘Windows’.
The code within the file gets executed the same way. The difference is that the extra ‘w’ tells Windows that the script is intended to run in the graphical interface, i.e., without a terminal window present during execution.
- Task Scheduler
Ideally, background scripts initiate on predefined triggers or run whenever the system is on, without the need to start them manually each time.
Task Scheduler is a refined piece of software included with Microsoft Windows, specifically designed to run tasks according to various criteria.
I will skip the detailed explanation and go straight to the point. However, if you need a detailed guide on its features and settings, here is a link to a Task Scheduler guide.
Create a script task in Task Scheduler
- Make sure that the Python script you are adding has a .pyw extension instead of .py.
- Open Task Scheduler by pressing Start and typing the initial letters of task scheduler until auto-complete suggests it.
- Select a Create Task action.
- In the General tab, give your task a name. Choose something short but descriptive. The name is required, and while the description is optional, it’s worth adding if you have many tasks or similar tasks.
- In the Triggers tab, add one or more triggers. For example, if you want the script to run at 10 AM every day, select On a schedule and specify the hour.
- In Actions tab, specify the tasks to be executed. Click the New button.
- Click Browse, navigate to the location of your virtual environment where the script is supposed to run. In the directory containing the activate file, there are .exe files for different Python executables. Locate pythonw.exe and select it. Be careful not to select python.exe by accident. The ‘w’ letter at the end is important, as we need to run a ‘.pyw’ file with ‘pythonw.exe’ for the script to run entirely in the background.
- In the Add arguments (optional) field, paste the full path to the script you want to run.
There are many other settings worth checking out to tailor the script to your needs, but the defaults are sensible and will generally not prevent the script from working.
It is generally worth manually starting the task after adding it to verify that it’s working before leaving Task Scheduler.
Final word
Mastering the art of running Python scripts silently in the background empowers users to automate routine tasks seamlessly. With Task Scheduler and a virtual environment, you can ensure scripts execute efficiently without cluttering your workspace. If you encounter any challenges or seek further customization, feel free to explore additional Task Scheduler features or reach out for assistance. Happy scripting!