如何找到我的 Python 站点包目录的位置? [英] How do I find the location of my Python site-packages directory?

查看:16
本文介绍了如何找到我的 Python 站点包目录的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到我的站点包目录的位置?

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

推荐答案

站点包目录有两种类型,全局每个用户.

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

  1. 全球站点包("dist-packages") 目录在运行时列在 sys.path 中:

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

python -m site

要获得更简洁的列表,请运行 getsitepackagesPython 代码中的>站点模块":

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

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

注意: virtualenvs getsitepackages 不可用,不过,上面的 sys.path 将正确列出 virtualenv 的 site-packages 目录.在 Python 3 中,您可以使用 sysconfig 模块 代替:

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"])'

  • 每个用户 站点包目录 (PEP 370) 是 Python 安装本地包的地方:

  • The per user site-packages directory (PEP 370) is where Python installs your local packages:

    python -m site --user-site
    

    如果这指向一个不存在的目录,请检查 Python 的退出状态并查看 python -m site --help 以获得解释.

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

    提示: 运行 pip list --userpip freeze --user 会为您提供每个用户的所有安装列表 站点包.

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

    <小时>

    实用技巧

    • .__path__ 可让您识别特定包的位置:(详情)


      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']
        

      • .__file__ 可让您识别特定模块的位置:(区别)

      • <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
        

      • 运行 pip show 以显示 Debian 风格的包信息:

      • 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
        

      • 这篇关于如何找到我的 Python 站点包目录的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆