Updated: · Automation Notes admin

A Python venv Inside OneDrive Can Break Your Scheduled Scripts

I ran a Python script manually and confirmed that it worked. Then, I registered it in Task Scheduler to run automatically every night. But when I checked the logs the next morning, it had stopped with a ModuleNotFoundError—even though it had definitely worked when I ran it manually.

This is a real issue I ran into. The cause was that I had created the Python virtual environment (venv) inside a OneDrive sync folder. In this article, I explain what happened and how to fix it.

What Happened

The situation was as follows:

The next morning, the logs showed No module named 'playwright' errors. I had configured it to retry on failure, but all seven retries failed with the exact same error. When I checked inside the venv, almost all libraries other than pip had vanished from the site-packages folder where libraries are installed.

Suspected Cause

I have not pinpointed the exact cause. However, looking at the circumstances, I believe that right after creating the venv, a conflict occurred while OneDrive was syncing a large number of small files, causing files to disappear. It is not uncommon for a venv with installed libraries to contain thousands of files.

In any case, there is little reason to place a venv in OneDrive. The official Python documentation says virtual environments are "not considered as movable or copyable – you just recreate the same environment in the target location." Even if synced to another computer, you cannot use them as-is. There is no need to keep something in a sync folder if there is no point in syncing it and it is hard to notice when it breaks.

You Might Be in OneDrive Without Realizing It

Even if you think "I didn't put it in OneDrive," you should check once. OneDrive for Windows has a feature that moves (redirects) familiar folders like Desktop and Documents to OneDrive. According to Microsoft's explanation, the targets are Desktop, Documents, Pictures, Screenshots, and Camera Roll. If this feature is enabled, a project created inside "Documents" is actually inside a OneDrive sync folder.

To check, open PowerShell in your project folder and run the following command. If the displayed path contains OneDrive, it is subject to syncing.

(Get-Location).Path

How to Fix It: Create Only the venv Outside OneDrive

You don't need to move the script files themselves. The only thing you need to move outside OneDrive is the venv.

ItemLocationReason
Script files (.py, .bat, etc.)Can stay inside OneDriveOnly a few small files, with the benefit of being backed up
requirements.txtCan stay inside OneDriveUsed to recreate the venv
venv (virtual environment)Move outside OneDriveContains many files with no point in syncing. Hard to notice if it breaks

I create a folder named .venvs under my user folder and keep each project's venv there. You can create one in PowerShell like this (replace myproject with your project's name):

python -m venv "$env:USERPROFILE\.venvs\myproject"
& "$env:USERPROFILE\.venvs\myproject\Scripts\python.exe" -m pip install -r requirements.txt

Once recreated, you can delete the old venv folder inside OneDrive. The official Python documentation also states that if you want to change locations, you should recreate it in the new location and delete the old one.

Call python.exe Directly Without Activating

When using a venv, tutorials often introduce the step of "activating" it by running Activate.ps1. However, under Windows' default execution policy (Restricted), running scripts is not permitted, so Activate.ps1 fails with an error.

In fact, activation is not necessary. The official Python documentation also states that activation is not strictly required if you specify the full path to the virtual environment's Python executable. In a batch file called by Task Scheduler, specify the venv's python.exe directly like this:

run.bat (called by Task Scheduler)

@echo off
"%USERPROFILE%\.venvs\myproject\Scripts\python.exe" "%~dp0main.py"
exit /b %ERRORLEVEL%

%~dp0 represents the folder where this batch file is located. This allows it to run while keeping the scripts inside OneDrive and the venv outside. The last line returns the exit code so that if Python fails, the task will also be recorded as failed.

Steps to Recreate

  1. 1. Create outsideCreate a new venv under %USERPROFILE%\.venvs\
  2. 2. ReinstallInstall the required libraries from requirements.txt
  3. 3. Update pathPoint the batch file's python.exe path to the new venv
  4. 4. Delete oldDelete the old venv inside OneDrive
The script files and requirements.txt remain inside OneDrive.

No Need to Move venvs You Run Manually

I only moved the venv used for daily automated runs. For projects you only run manually once in a while, even if it breaks, you will notice on the spot and can simply recreate it. The real issue is unattended venvs where no one notices if they break. When setting up automated runs, it is enough to just check where that venv is placed.

Summary

I also wrote about running Claude Code every day with Windows Task Scheduler, currently in Japanese only.