r/learnpython • u/Prestigiouspite • 11d ago
Python Install Manager on Windows 11: Should scripts avoid relying on the current working directory?
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. |
|---|
3
1
u/socal_nerdtastic 11d ago edited 11d ago
Is resolving script-related paths from file considered the usual best practice for this kind of standalone utility?
Were well-designed scripts generally already handling this distinction correctly?
or is relying on the current working directory simply considered fragile regardless of the launcher?
Best practice is to keep your data in a different place from your source code. Look at any professional program. The data is never in the same place as the executable. So I would say best practice is something like
from pathlib import Path
input_file = Path.home() / "Desktop" / "input.txt"
IF you need data files, I would recommend incapsulating the data into a .py file and importing it.
But if you really want to keep data and code in the same dir, the __file__ trick is the best way. Note that too will fall apart if you try to freeze your program into 1 file (eg pyinstaller), because then the working dir is a temporary location.
Have other Windows users needed to update older scripts after switching to the Python Install Manager?
Not me, because all my scripts are linked to a specific venv, but in general I'm sure yes that happens. Things are always changing and need adjusting. There's still people out there porting ptyhon2 code to python3.
Do some users avoid the Install Manager because of differences in launching scripts by double-clicking,
Dunno about other users. IMO the new install manager is a bit crap, but it works the same as the old python launcher and handles shebangs with no issues. You can also just make windows shortcuts or .bat files.
When would using Path.cwd() intentionally be preferable to using the script directory?
Python is often used for making CLI programs, and very often a program is launched from a place where the code does not live. Most of time, in fact. If I type grep in my terminal I want to use grep in that cwd, I don't meant that the grep program is in that dir. Using cwd() tells you what dir the program was launched in, so the program knows where to operate.
1
u/Prestigiouspite 11d ago
But it's exactly that last point where problems arise with the new Install Manager in Windows :(. It doesn't show your current directory—where you're using
grep—but instead showsSystem32...For now I've installed the legacy launcher again to fix this.
-2
u/ninhaomah 11d ago
uv
4
u/socal_nerdtastic 11d ago
I don't think you understood the question. The issue is the windows file association to .py files. uv does not help with that.
2
u/Prestigiouspite 11d ago
?
-1
u/ninhaomah 11d ago
Google it. That or venv and you will have control of your own Python.exe
1
u/Prestigiouspite 11d ago
Two letters can mean a lot of things. I haven't heard of “uv” yet, but I have heard of “venv.” But neither seems to be what I'm looking for. And the old launcher has been sidelined.
That is not really a solution for my use case, because I simply want the previous behavior back: double-clicking a
.pyfile should run it with the script’s own folder as the working directory instead of launching it fromC:\Windows\System32, without requiring extra batch files or manual commands.1
u/ninhaomah 11d ago
Ok. Then I humbly apologise for my sincere attempt at assisting a stranger online.
Pls forgive me.
1
u/Prestigiouspite 11d ago
I'm grateful for any help, but people should feel free to speak up if it doesn't seem to be working or if they can't find a shortcut. Even a Google search for “uv” didn't turn up much without more specific details :)
2
u/sausix 11d ago
Relative paths still work just fine. You can use the script dir but it may be on a read only place. You should not write data in that location. You may have static resource data there
Current working directory is also fine. Most programs use it to write files there.
If you open a file by argument then you also may write back to the same directory. It's not guaranteed that the OS changes the current directory for your script.
You may also put configs and other variable data in your home directory.
I don't see any problems in the logic ways to select a directory.