首页 > 系统相关 >Conda in Windows under MSYS2 and Zsh 的问题解决

Conda in Windows under MSYS2 and Zsh 的问题解决

时间:2023-03-28 13:00:20浏览次数:69  
标签:__ MSYS2 Windows SYSP CONDA activate conda PATH Conda

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 activate UnicodeEncodeError if non-ascii characters present in bash prompt · Issue #7876 · conda/conda (github.com)

尝试过程:

  1. # >>> 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 <<<
    
  2. 使用大佬分享的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

标签:__,MSYS2,Windows,SYSP,CONDA,activate,conda,PATH,Conda
From: https://www.cnblogs.com/kphang/p/17264764.html

相关文章

  • windows下使用docker
    之前一直在Linux上玩docker,这次尝试下在windows上操作docker。做下总结:首先启动dockerdesktop。1、VS中右键-发布项目 2、在dockerfile所在目录下执行dockerbuil......
  • Installing Windows 7 on VirtualBox. Error status: 0xc0000225?
    Today,afterfinallytakingtheplungeintotheLinuxworld(I'mtryingtorefocusmyworktotheRailsecosystem),IinstalledVirtualBoxonLinuxMint13. Si......
  • Windows启动一个FTP站点,让局域网的人访问/写
      现在需要一台局域网的电脑启动一个ftp站点,该电脑需要进行以下步骤:https://blog.csdn.net/m0_50298323/article/details/128404598   ......
  • Windows设置自动登录
    直接修改注册表我发现有的Windows10版本,运行netplwiz,出现的“用户账户”的对话框中并没有以上选项,这可能由于在系统安装或者配置过程的某种设置问题造成的。这种情况应......
  • 小皮Windows web面板漏洞详解
    漏洞简介PhpStudy国内12年老牌公益软件,集安全、高效、功能与一体,已获得全球用户认可安装,运维也高效。支持一键LAMP、LNMP、集群、监控、网站、数据库、FTP、软件中心......
  • Windows _popen 会吃了前后双引号
    Windows_popen函数会吃了前后双引号.例如,"c:/asd/blhost"-u--write-memory0x70010000"c:/asd/asd.bin"会被吃掉前后引号变为c:/asd/blhost"-u--write-me......
  • 就系统周期问题,提问微软Windows
    Windows7让微软又开始用数字命名了,当然接下来的是Windows8,以前各种命名都是数字和名词混搭。Windows2000(4),XP(5),Vista(6),然后才是Windows7和现在的Windows8。给出了这一......
  • Windows 8将包含电话呼叫功能
    Windows8会让你打电话,直接发送短信给亲戚朋友吗?一系列的证据表明微软正在研发一项Windows8新功能,它允许Windows8用户直接从操作系统呼叫其他用户。 istartedsomething......
  • iPad vs Windows 8 平板【视屏】
    本星期的早些时候微软已经发布了Windows8开发者预览版。相信会有不少朋友会像我一样很想知道Windows8和 iOS相比结果会是如何?微软在Window8上已经做了些让人印象深......
  • Metro让我们看到Windows平板的希望,但仍需开发者支持
    微软的下一代操作系统Windows8具有平板电脑和台式机的双界面。Windows8的“Metro”界面由许多“砖块”组成,便于使用和触摸操作。如同苹果的Appstore一样,应用开发者可以......