Conda in Windows under MSYS2 and Zsh 的问题解决
Conda in Windows under MSYS2 and Zsh 的问题解决
在Window11上使用git bash 安装zsh,并配置p10k主题,主要问题就是prompt中无法显示conda env;conda activate/deactivate 命令不能正常使用;
总结其实就是一个核心问题,\r,\n,\r\n的区别;
Errors likes these:
(eval):11: parse error near ^M
\r
被解析成了^M
,导致路径错误,如下:/c/Users/joaom/Miniconda3/Scripts/conda.exe^M
~
❯ conda activate base
~ [base]
❯ conda deactivate
__conda_activate:13: no such file or directory: /c/Users/joaom/Miniconda3/Scripts/conda.exe^M
或者,conda init 无效:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
或者一些其他的报错,是关于window 和 unix编码的问题。
参考;
Conda in Windows under MSYS2 and Zsh line ending problems · Issue #9922 · conda/conda (github.com)
尝试过程:
-
# >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! #if [ -f '/d/Anaconda3/Scripts/conda.exe' ]; then # eval "$('/d/Anaconda3/Scripts/conda.exe' 'shell.zsh' 'hook')" #fi # temp solution . /d/Anaconda3/etc/profile.d/conda.sh # <<< conda initialize <<<
-
使用大佬分享的conda.sh文件;
export CONDA_EXE=$(echo '/cygdrive/c/Users/karp/anaconda3/Scripts/conda.exe' | tr -d '\r') export _CE_M='' export _CE_CONDA='' export CONDA_PYTHON_EXE='C:\Users\karp\anaconda3\python.exe' # Copyright (C) 2012 Anaconda, Inc # SPDX-License-Identifier: BSD-3-Clause __add_sys_prefix_to_path() { # In dev-mode CONDA_EXE is python.exe and on Windows # it is in a different relative location to condabin. if [ -n "${_CE_CONDA}" ] && [ -n "${WINDIR+x}" ]; then SYSP=$(\dirname "${CONDA_EXE}") SYSP=$(echo ${SYSP} | tr -d '\r') else SYSP=$(\dirname "${CONDA_EXE}") SYSP=$(\dirname "${SYSP}") SYSP=$(echo ${SYSP} | tr -d '\r') fi if [ -n "${WINDIR+x}" ]; then PATH="${SYSP}/bin:${PATH}" PATH="${SYSP}/Scripts:${PATH}" PATH="${SYSP}/Library/bin:${PATH}" PATH="${SYSP}/Library/usr/bin:${PATH}" PATH="${SYSP}/Library/mingw-w64/bin:${PATH}" PATH="${SYSP}:${PATH}" PATH=$(echo ${PATH} | tr -d '\r') else PATH="${SYSP}/bin:${PATH}" PATH=$(echo ${PATH} | tr -d '\r') fi \export PATH } __conda_exe() ( __add_sys_prefix_to_path $(echo "$CONDA_EXE" | tr -d '\r') $_CE_M $_CE_CONDA $(echo "$@" | tr -d '\r') ) __conda_hashr() { if [ -n "${ZSH_VERSION:+x}" ]; then \rehash elif [ -n "${POSH_VERSION:+x}" ]; then : # pass else \hash -r fi } __conda_activate() { if [ -n "${CONDA_PS1_BACKUP:+x}" ]; then # Handle transition from shell activated with conda <= 4.3 to a subsequent activation # after conda updated to >= 4.4. See issue #6173. PS1="$CONDA_PS1_BACKUP" PS1=$(echo ${PS1} | tr -d '\r') \unset CONDA_PS1_BACKUP fi \local ask_conda ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix "$@")" || \return ask_conda=$(echo ${ask_conda} | tr -d '\r') \eval "$ask_conda" __conda_hashr } __conda_reactivate() { \local ask_conda ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix reactivate)" || \return ask_conda=$(echo ${ask_conda} | tr -d '\r') \eval "$ask_conda" __conda_hashr } conda() { \local cmd="${1-__missing__}" case "$cmd" in activate|deactivate) __conda_activate $(echo "$@" | tr -d '\r') ;; install|update|upgrade|remove|uninstall) __conda_exe $(echo "$@" | tr -d '\r') || \return __conda_reactivate ;; *) __conda_exe $(echo "$@" | tr -d '\r') ;; esac } if [ -z "${CONDA_SHLVL+x}" ]; then \export CONDA_SHLVL=0 # In dev-mode CONDA_EXE is python.exe and on Windows # it is in a different relative location to condabin. if [ -n "${_CE_CONDA:+x}" ] && [ -n "${WINDIR+x}" ]; then PATH="$(\dirname "$CONDA_EXE")/condabin${PATH:+":${PATH}"}" else PATH="$(\dirname "$(\dirname "$CONDA_EXE")")/condabin${PATH:+":${PATH}"}" fi \export PATH # We're not allowing PS1 to be unbound. It must at least be set. # However, we're not exporting it, which can cause problems when starting a second shell # via a first shell (i.e. starting zsh from bash). if [ -z "${PS1+x}" ]; then PS1= fi fi
目前仍然存在的问题是:
虽然conda activate/deactivate 命令不报错了,但是非base的虚拟环境并未完全激活;
如下,base env 正常,但其他的env 就不行;(注意*,表示当前激活环境)
❯ conda activate base
❯ conda env list
# conda environments:
#
base * D:\Anaconda3
d2l D:\Anaconda3\envs\d2l
非base env 的情况:(并未激活;)
❯ conda activate d2l
❯ conda env list
# conda environments:
#
base D:\Anaconda3
d2l D:\Anaconda3\envs\d2l
后来又找到:https://github.com/conda/conda/issues/9922#issuecomment-1361695031
完美解决。
❯ conda activate d2l
❯ conda env list
# conda environments:
#
base D:\Anaconda3
d2l * D:\Anaconda3\envs\d2l
作者:kphang
出处:https://www.cnblogs.com/kphang/p/17264764.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
备注:你可以在这里自定义其他内容,支持 HTML
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理