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

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

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

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

    You should try this command to determine pip's install location

    Python 2

    pip show six | grep "Location:" | cut -d " " -f2
    

    Python 3

    pip3 show six | grep "Location:" | cut -d " " -f2
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 03:33

    All the answers (or: the same answer repeated over and over) are inadequate. What you want to do is this:

    from setuptools.command.easy_install import easy_install
    class easy_install_default(easy_install):
      """ class easy_install had problems with the fist parameter not being
          an instance of Distribution, even though it was. This is due to
          some import-related mess.
          """
    
      def __init__(self):
        from distutils.dist import Distribution
        dist = Distribution()
        self.distribution = dist
        self.initialize_options()
        self._dry_run = None
        self.verbose = dist.verbose
        self.force = None
        self.help = 0
        self.finalized = 0
    
    e = easy_install_default()
    import distutils.errors
    try:
      e.finalize_options()
    except distutils.errors.DistutilsError:
      pass
    
    print e.install_dir
    

    The final line shows you the installation dir. Works on Ubuntu, whereas the above ones don't. Don't ask me about windows or other dists, but since it's the exact same dir that easy_install uses by default, it's probably correct everywhere where easy_install works (so, everywhere, even macs). Have fun. Note: original code has many swearwords in it.

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

    This should work on all distributions in and out of virtual environment due to it's "low-tech" nature. The os module always resides in the parent directory of 'site-packages'

    import os; print(os.path.dirname(os.__file__) + '/site-packages')
    

    To change dir to the site-packages dir I use the following alias (on *nix systems):

    alias cdsp='cd $(python -c "import os; print(os.path.dirname(os.__file__))"); cd site-packages'
    
    0 讨论(0)
  • 2020-11-22 03:33

    Answer to old question. But use ipython for this.

    pip install ipython
    ipython 
    import imaplib
    imaplib?
    

    This will give the following output about imaplib package -

    Type:        module
    String form: <module 'imaplib' from '/usr/lib/python2.7/imaplib.py'>
    File:        /usr/lib/python2.7/imaplib.py
    Docstring:  
    IMAP4 client.
    
    Based on RFC 2060.
    
    Public class:           IMAP4
    Public variable:        Debug
    Public functions:       Internaldate2tuple
                            Int2AP
                            ParseFlags
                            Time2Internaldate
    
    0 讨论(0)
  • 2020-11-22 03:34

    This is what worked for me:

    python -m site --user-site
    
    0 讨论(0)
提交回复
热议问题