I installed Python 3.13 on Windows 11 with
winget install python3 --scope machine`
Then I installed pip
with
python -m ensurepip
This puts pip
in %APPDATA\Python\Python313\site-packages
. Since this directory is in sys.path
, I can run pip
like this:
python -m pip ...
This is great: the command is easy to remember, and isn't going to change when I upgrade Python.
However, if the package includes an executable or script, these go in %APPDATA%\Python\Python313\Scripts
. This is not automatically added to %PATH%
by the Python installer. And since the Python version number changes, I can't just hardcode this path in batch scripts.
I also checked site.USER_BASE
but it is set to %APPDATA%\Python
which doesn't include the Python version. On the other hand site.USER_SITE
includes the Python version and \site-packages
.
Is there an easy way to run package scripts with Python on Windows without expecting the end user to modify %PATH%
?
I installed Python 3.13 on Windows 11 with
winget install python3 --scope machine`
Then I installed pip
with
python -m ensurepip
This puts pip
in %APPDATA\Python\Python313\site-packages
. Since this directory is in sys.path
, I can run pip
like this:
python -m pip ...
This is great: the command is easy to remember, and isn't going to change when I upgrade Python.
However, if the package includes an executable or script, these go in %APPDATA%\Python\Python313\Scripts
. This is not automatically added to %PATH%
by the Python installer. And since the Python version number changes, I can't just hardcode this path in batch scripts.
I also checked site.USER_BASE
but it is set to %APPDATA%\Python
which doesn't include the Python version. On the other hand site.USER_SITE
includes the Python version and \site-packages
.
Is there an easy way to run package scripts with Python on Windows without expecting the end user to modify %PATH%
?
- Is there any way to put your script in a place that's already in the path? Obviously pip is able to do it. – Mark Ransom Commented Mar 7 at 1:44
- You could force pip to install to `C:\Program Files\Python313\Scripts` which is already in PATH. But by default it does not install there. I don't know if it requires administrator privileges. – rgov Commented Mar 7 at 18:28
1 Answer
Reset to default 0The path can be determined from the sysconfig
module like so:
sysconfig.get_path('scripts', sysconfig.get_preferred_scheme('user'))
On Windows, sysconfig.get_preferred_scheme('user') == 'nt_user'
.
In the Command Prompt this would be:
for /f "delims=" %A in ('python -c "print(__import__('sysconfig').get_path('scripts', 'nt_user'))"') do @set PATH=%PATH%;%A
scriptname.exe
In Powershell:
$env:PATH = (python -c "import sysconfig; print(sysconfig.get_path('scripts', 'nt_user'))") + ";" + $env:PATH
scriptname.exe