最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Deriving path of Python Scripts folder on Windows - Stack Overflow

programmeradmin1浏览0评论

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%?

Share Improve this question edited Mar 6 at 19:51 rgov asked Mar 5 at 23:30 rgovrgov 4,4092 gold badges37 silver badges75 bronze badges 2
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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
发布评论

评论列表(0)

  1. 暂无评论