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

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

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

相关标签:
21条回答
  • 2020-11-22 03:25

    There are two types of site-packages directories, global and per user.

    1. Global site-packages ("dist-packages") directories are listed in sys.path when you run:

      python -m site
      

      For a more concise list run getsitepackages from the site module in Python code:

      python -c 'import site; print(site.getsitepackages())'
      

      Note: With virtualenvs getsitepackages is not available, sys.path from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:

      python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
      
    2. The per user site-packages directory (PEP 370) is where Python installs your local packages:

      python -m site --user-site
      

      If this points to a non-existing directory check the exit status of Python and see python -m site --help for explanations.

      Hint: Running pip list --user or pip freeze --user gives you a list of all installed per user site-packages.


    Practical Tips

    • <package>.__path__ lets you identify the location(s) of a specific package: (details)

      $ python -c "import setuptools as _; print(_.__path__)"
      ['/usr/lib/python2.7/dist-packages/setuptools']
      
    • <module>.__file__ lets you identify the location of a specific module: (difference)

      $ python3 -c "import os as _; print(_.__file__)"
      /usr/lib/python3.6/os.py
      
    • Run pip show <package> to show Debian-style package information:

      $ pip show pytest
      Name: pytest
      Version: 3.8.2
      Summary: pytest: simple powerful testing with Python
      Home-page: https://docs.pytest.org/en/latest/
      Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
      Author-email: None
      License: MIT license
      Location: /home/peter/.local/lib/python3.4/site-packages
      Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
      
    0 讨论(0)
  • 2020-11-22 03:25

    pip show will give all the details about a package: https://pip.pypa.io/en/stable/reference/pip_show/ [pip show][1]

    To get the location:

    pip show <package_name>| grep Location
    
    0 讨论(0)
  • 2020-11-22 03:27

    Something that has not been mentioned which I believe is useful, if you have two versions of Python installed e.g. both 3.8 and 3.5 there might be two folders called site-packages on your machine. In that case you can specify the python version by using the following:

    py -3.5 -c "import site; print(site.getsitepackages()[1])
    
    0 讨论(0)
  • 2020-11-22 03:30
    >>> import site; site.getsitepackages()
    ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
    

    (or just first item with site.getsitepackages()[0])

    0 讨论(0)
  • 2020-11-22 03:31

    The native system packages installed with python installation in Debian based systems can be found at :

    /usr/lib/python2.7/dist-packages/

    In OSX - /Library/Python/2.7/site-packages

    by using this small code :

    from distutils.sysconfig import get_python_lib
    print get_python_lib()
    

    However, the list of packages installed via pip can be found at :

    /usr/local/bin/

    Or one can simply write the following command to list all paths where python packages are.

    >>> import site; site.getsitepackages()
    ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
    

    Note: the location might vary based on your OS, like in OSX

    >>> import site; site.getsitepackages()
    ['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']
    
    0 讨论(0)
  • 2020-11-22 03:31
    from distutils.sysconfig import get_python_lib
    print get_python_lib()
    
    0 讨论(0)
提交回复
热议问题