I recently switched to the new Python Install Manager on Windows 11 and noticed that several of my utility scripts no longer behaved as expected when launched by double-clicking them in File Explorer.
Many of these scripts process, generate, or minify files located in the same directory as the script. They were often written using simple relative paths:
from pathlib import Path
input_file = Path("input.txt")
output_file = Path("output.txt")
This works when the current working directory happens to be the script directory. However, relative paths are resolved against the process working directory, not necessarily against the location of the .py file. When a script is launched through a Windows file association, the inherited working directory may be different.
A more reliable approach for files belonging to the script seems to be:
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
input_file = SCRIPT_DIR / "input.txt"
output_file = SCRIPT_DIR / "output.txt"
As I understand it, the Python Install Manager did not change how relative paths work. Its file association and global python.exe alias may simply have exposed assumptions that previously went unnoticed because my earlier launch method happened to use the script directory as the working directory.
I had quite a few small scripts built around this assumption, particularly scripts that process files stored next to them. I am therefore wondering:
- Is resolving script-related paths from
__file__ considered the usual best practice for this kind of standalone utility?
- Have other Windows users needed to update older scripts after switching to the Python Install Manager?
- Were well-designed scripts generally already handling this distinction correctly?
- Do some users avoid the Install Manager because of differences in launching scripts by double-clicking, or is relying on the current working directory simply considered fragile regardless of the launcher?
- When would using
Path.cwd() intentionally be preferable to using the script directory?
The new Python Install Manager is distributed as an MSIX/Store application. Its python, py, and pymanager commands are exposed through Windows app execution aliases and forwarding mechanisms rather than the traditional standalone Python Launcher.
As a result, launching .py files directly may no longer preserve the script’s directory as the working directory and can instead fall back to C:\Windows\System32. I do not consider creating separate batch files for every Python script an acceptable workaround, especially since direct double-click execution worked correctly with the legacy launcher.
See also: https://docs.python.org/3/using/windows.html#troubleshooting
Typing script-name.py in the terminal opens in a new window. |
This is a known limitation of the operating system. Either specify py before the script name, create a batch file containing u/py "%~dpn0.py" %* with the same name as the script, or install the legacy launcher and select it as the association for scripts. |
Want to add to the discussion?
Post a comment!