Python环境清理(MacOS)
零碎知识 Python 0 367

本文考虑的Python来源有以下三种:

搜索

  1. 检查当前系统中存在的python解释器
    ls /usr/local/bin | grep "python"
    # ================================
    ipython
    ipython3
    python
    python3
    python3-config
    python3-intel64
    python3.11
    python3.11-config
    python3.12
    python3.12-config
    python3.12-intel64
    python3.9
    python3.9-config
    # NOTE: 这是个系统级python解释器,对应的python不建议删除
    ls /usr/bin | grep "python" 
    # ================================
    python3
    brew list | grep "python"
    # ================================
    ipython
    python-certifi
    python-click
    python-lxml
    python-markupsafe
    python-packaging
    python-pyparsing
    python-pytz
    python-setuptools
    python@3.11
    python@3.12
    python@3.9
  2. 找到对应的python可执行文件路径【可执行文件就在安装路径内】
    # 交互式的进入python后
    >>> import sys
    >>> sys.executable
    此时,输出可能是真实的执行文件路径,如['/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages', '/Library/Python/3.9/site-packages', '/AppleInternal/Library/Python/3.9/site-packages', '/AppleInternal/Tests/Python/3.9/site-packages'],也可能是一个软连接,如'/usr/local/bin/python3'
    如果是软链接,还需要进一步找到真实的路径,使用readlink找到真实路径【可能需要迭代使用】
    readlink /usr/local/bin/python3
    # ================================
    ../../../Library/Frameworks/Python.framework/Versions/3.12/bin/python3
  3. 找到对应的python第三方依赖安装路径【brew安装python会自己管理第三方依赖】
    # 交互式的进入python后
    >>> import site
    >>> site.getsitepackages()

经测试,按照来源,相关路径可作如下分类:

  1. 官网:
    • 安装路径: /Library/Frameworks/Python.framework/Versions/3.x
    • 第三方依赖路径: /Library/Frameworks/Python.framework/Versions/3.x/lib/python3.x/site-packages
    • 相关工具路径: /Applications/Python 3.x
  2. brew:
    • 安装路径: /usr/local/Cellar/python@3.x
    • 第三方依赖路径: /usr/local/Cellar/python@3.x/3.x.y/Frameworks/Python.framework/Versions/3.x/lib/python3.x/site-packages
    • 软链接: /usr/local/opt/python@3.x
  3. Command Line Tools:
    • /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework
  4. 其他:
    • brew中的six
      • /usr/local/lib/python3.x/site-packages
    • 往期版本残留位置
      • /Users/[username]/Library/Python/3.x/lib/python/site-packages

清理

推荐按照如下几条规则:

  • brew下载的python包不需要自己管理,只用做其他软件包的依赖
  • ipython之类的也不要通过brew安装,因为扩展很不方便
  • 官网下载的安装包用于日常使用
    • 安装ipython用于计算器
    • 安装matplotlib用于日常绘图
    • 安装pipdeptreepip-review用于依赖管理
  • 特定项目的python使用virtualenv创建
  • 软硬链接(快捷方式)规则:
    • /usr/local/bin只保存官网的解释器与pip软链接
    • /usr/bin/只保存CommandLineTools的解释器与pip硬链接
    • 确保命令兼容:
      • 创建/usr/local/bin/python软链接: ln -s /usr/local/bin/python3 /usr/local/bin/python
      • 创建/usr/local/bin/pip软链接: ln -s /usr/local/bin/pip3 /usr/local/bin/pip
编写
预览