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

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

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

21条回答
  •  感情败类
    2020-11-22 03:16

    As others have noted, distutils.sysconfig has the relevant settings:

    import distutils.sysconfig
    print distutils.sysconfig.get_python_lib()
    

    ...though the default site.py does something a bit more crude, paraphrased below:

    import sys, os
    print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])
    

    (it also adds ${sys.prefix}/lib/site-python and adds both paths for sys.exec_prefix as well, should that constant be different).

    That said, what's the context? You shouldn't be messing with your site-packages directly; setuptools/distutils will work for installation, and your program may be running in a virtualenv where your pythonpath is completely user-local, so it shouldn't assume use of the system site-packages directly either.

提交回复
热议问题