Programmatically determine pip 'user install' location Scripts directory

后端 未结 2 2061
梦如初夏
梦如初夏 2020-12-21 15:27

As explained in pip\'s documentation a user can install packages in his personal account using pip install --user .

How can I programmatical

相关标签:
2条回答
  • 2020-12-21 15:31

    Command-line:

    python -c "import os, site; print(os.path.join(site.USER_BASE, 'Scripts' if os.name == 'nt' else 'bin'))"
    

    Function:

    import os, site
    
    if os.name == 'nt':
        bin_dir = 'Scripts'
    else:
        bin_dir = 'bin'
    
    def get_user_install_bin_dir():
        return os.path.join(site.USER_BASE, bin_dir)
    
    0 讨论(0)
  • 2020-12-21 15:33

    I believe the following should give the expected result

    import os
    import sysconfig
    
    user_scripts_path = sysconfig.get_path('scripts', f'{os.name}_user')
    print(user_scripts_path)
    

    But it might be that pip uses a different logic internally (probably based on distutils), but the results should still be the same.

    References

    • https://docs.python.org/3/library/sysconfig.html#installation-paths
    • https://docs.python.org/3/library/os.html#os.name
    0 讨论(0)
提交回复
热议问题