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

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

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

21条回答
  •  醉梦人生
    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.

提交回复
热议问题