升级MacOS Mojave后无法在Emacs Shell中激活Conda环境及Pip异常求助
我之前升级Mojave后也碰到过类似的环境问题,结合你的报错信息,整理了几个能快速解决的步骤:
问题根源
核心问题有两个:一是Emacs的Shell默认不会加载系统终端的完整环境变量,导致Conda和virtualenvwrapper的初始化脚本没生效;二是旧版本的Pip和升级后的环境不兼容,引发了导入错误。
具体解决方案
1. 让Emacs Shell加载完整的系统环境
Emacs启动的Shell默认是非登录式的,不会读取.bash_profile或.zshrc这类配置文件,而Conda和virtualenvwrapper的初始化代码通常都写在这些文件里。你可以修改Emacs配置来解决:
打开你的Emacs配置文件(比如~/.emacs.d/init.el),添加以下代码:
;; 配置Shell为登录交互式模式,加载系统环境 (defun setup-shell-environment () (setq shell-file-name "/bin/bash") ;; 如果你用zsh就改成"/bin/zsh" (setq explicit-shell-file-name shell-file-name) (setq explicit-bash-args '("--login" "-i")) ) (add-hook 'shell-mode-hook 'setup-shell-environment) ;; 另一种更简便的方式(需要先安装exec-path-from-shell包) (when (memq window-system '(mac ns)) (exec-path-from-shell-initialize))
如果用第二种方式,记得先通过MELPA安装exec-path-from-shell包。重启Emacs后,再打开Shell应该就能识别Conda环境了。
2. 修复Pip的版本兼容性错误
你遇到的ImportError: cannot import name 'InvalidSchemeCombination'是因为Pip版本太旧,和Python 3.7不匹配。先在系统终端(这里环境正常)里执行以下命令更新Pip到兼容版本:
conda activate base pip install --upgrade pip==20.3.4
选20.3.4是因为它是支持Python 3.7的最后几个稳定版本,不会出现兼容性问题。
3. 确认virtualenvwrapper的安装位置
如果还是弹出virtualenvwrapper的报错,先在系统终端里检查它是否安装在Conda的base环境中:
pip show virtualenvwrapper
确保输出的Location是/Users/user/miniconda3/lib/python3.7/site-packages,如果不是,重新安装:
pip install virtualenvwrapper
验证方法
- 重启Emacs,打开
M-x shell - 尝试激活你的Conda环境:
conda activate your-env-name - 执行
pip --version,确认没有报错且版本显示为20.3.4
内容的提问来源于stack exchange,提问作者MadPhysicist

