How do I find the location of my Python site-packages directory?

前端 未结 21 2682
梦谈多话
梦谈多话 2020-11-22 03:06

How do I find the location of my site-packages directory?

21条回答
  •  温柔的废话
    2020-11-22 03:33

    A modern stdlib way is using sysconfig module, available in version 2.7 and 3.2+. Unlike the current accepted answer, this method still works regardless of whether or not you have a virtual environment active.

    Note: sysconfig (source) is not to be confused with the distutils.sysconfig submodule (source) mentioned in several other answers here. The latter is an entirely different module and it's lacking the get_paths function discussed below.

    Python currently uses eight paths (docs):

    • stdlib: directory containing the standard Python library files that are not platform-specific.
    • platstdlib: directory containing the standard Python library files that are platform-specific.
    • platlib: directory for site-specific, platform-specific files.
    • purelib: directory for site-specific, non-platform-specific files.
    • include: directory for non-platform-specific header files.
    • platinclude: directory for platform-specific header files.
    • scripts: directory for script files.
    • data: directory for data files.

    In most cases, users finding this question would be interested in the 'purelib' path (in some cases, you might be interested in 'platlib' too). The purelib path is where ordinary Python packages will be installed by tools like pip.

    At system level, you'll see something like this:

    # Linux
    $ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"
    /usr/local/lib/python3.8/site-packages
    
    # macOS (brew installed python3.8)
    $ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"
    /usr/local/Cellar/python@3.8/3.8.3/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
    
    # Windows
    C:\> py -c "import sysconfig; print(sysconfig.get_path('purelib'))"
    C:\Users\wim\AppData\Local\Programs\Python\Python38\Lib\site-packages
    

    With a venv, you'll get something like this

    # Linux
    /tmp/.venv/lib/python3.8/site-packages
    
    # macOS
    /private/tmp/.venv/lib/python3.8/site-packages
    
    # Windows
    C:\Users\wim\AppData\Local\Temp\.venv\Lib\site-packages
    

    The function sysconfig.get_paths() returns a dict of all of the relevant installation paths, example on Linux:

    >>> import sysconfig
    >>> sysconfig.get_paths()
    {'stdlib': '/usr/local/lib/python3.8',
     'platstdlib': '/usr/local/lib/python3.8',
     'purelib': '/usr/local/lib/python3.8/site-packages',
     'platlib': '/usr/local/lib/python3.8/site-packages',
     'include': '/usr/local/include/python3.8',
     'platinclude': '/usr/local/include/python3.8',
     'scripts': '/usr/local/bin',
     'data': '/usr/local'}
    

    A shell script is also available to display these details, which you can invoke by executing sysconfig as a module:

    python -m sysconfig
    

提交回复
热议问题