You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

M1芯片MacOS系统Python虚拟环境与模块导入故障求助

彻底清理并重新搭建Python/Jupyter环境方案(M1 Mac Monterey 12.2)

一、完全清理现有混乱环境

  1. 删除旧虚拟环境
    找到你之前创建的venv文件夹,直接删除:
rm -rf /path/to/your/old/venv
  1. 卸载Jupyter相关组件
    先卸载pip安装的Jupyter包:
pip3 uninstall -y jupyter jupyterlab notebook ipython ipykernel

若通过Homebrew安装过Jupyter,一并卸载:

brew uninstall --force jupyter jupyterlab
  1. 彻底卸载Homebrew版Python 3.11
brew uninstall --force python@3.11
# 清理残留文件
rm -rf /opt/homebrew/Cellar/python@3.11
rm -rf /opt/homebrew/lib/python3.11
rm -rf ~/.local/share/jupyter
rm -rf ~/.jupyter
rm -rf /opt/homebrew/share/jupyter
# 清理之前创建的软链接
rm -rf /opt/homebrew/Cellar/python@3.11/3.11.6/Frameworks/Python.framework/Versions/3.11/share/jupyter
  1. 清理pip缓存
rm -rf ~/.cache/pip

二、重新搭建干净环境

  1. 更新Homebrew
brew update
  1. 安装Python 3.11
brew install python@3.11

验证安装:

which python3  # 应输出 /opt/homebrew/bin/python3
python3 --version  # 应显示 3.11.6
which pip3  # 应输出 /opt/homebrew/bin/pip3
  1. 按需选择全局或虚拟环境安装Jupyter
  • 全局环境(不推荐,易混乱):
    pip3 install jupyter jupyterlab notebook ipykernel
    
  • 虚拟环境(推荐,隔离课程依赖):
    创建新的虚拟环境:
    python3 -m venv /path/to/new/venv
    
    激活虚拟环境:
    source /path/to/new/venv/bin/activate
    
    在激活的环境中安装课程所需模块和Jupyter:
    pip install jupyter [your-course-packages]
    
    启动Jupyter:
    jupyter notebook
    

三、配置PyCharm和Kate

  1. PyCharm:
    打开项目后,进入File > Settings > Project: [your-project] > Python Interpreter,点击齿轮图标选择Add,在弹出窗口中选择Existing environment,找到新venv的Python路径(/path/to/new/venv/bin/python),确认即可。
  2. Kate:
    方式一:在终端激活venv后启动Kate:
source /path/to/new/venv/bin/activate && open -a Kate

方式二:在Kate的终端设置中,将启动命令改为上述命令,确保每次打开Kate都使用指定虚拟环境。

关键注意事项

  • 避免在虚拟环境中调用全局Jupyter,务必在激活的venv内安装并启动Jupyter,确保环境隔离。
  • 若后续需要多个课程环境,为每个课程单独创建venv,避免依赖冲突。

内容的提问来源于stack exchange,提问作者chococrepe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 19:50:07