我如何在MSYS2 MinTTY中使用Miniconda / Anaconda Python

问题描述 投票:0回答:1

我非常喜欢MSYS2 Shell。我使用MSYS2软件包通过MinGW-W64编译器构建内容。 MSYS2中还提供了Python安装,但是我无法使用pip安装conda。有什么方法可以在MSYS2环境中使用Miniconda中的python和conda吗?

python anaconda msys2 miniconda
1个回答
0
投票

将miniconda下载并安装到C:\miniconda3

    将MSYS2下载并安装到C:\msys64中(并执行pacman -Syu两次)。
  1. C:\miniconda3\etc\profile.d\conda.sh的标题修改为:
  2. export CONDA_EXE='/c/miniconda3/Scripts/conda.exe' export _CE_M='' export _CE_CONDA='' export CONDA_PYTHON_EXE='/c/miniconda3/python.exe'
  3. 当使用具有完整路径的环境时,我们需要转义bash的$ PS1中的Windows路径分隔符,以便正确显示它。将__conda_activate中的C:\miniconda3\etc\profile.d\conda.sh功能更改为:
  4. __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" \unset CONDA_PS1_BACKUP fi \local cmd="$1" shift \local ask_conda CONDA_INTERNAL_OLDPATH="${PATH}" __add_sys_prefix_to_path ask_conda="$(PS1="$PS1" "$CONDA_EXE" $_CE_M $_CE_CONDA shell.posix "$cmd" "$@")" || \return $? rc=$? PATH="${CONDA_INTERNAL_OLDPATH}" # here the ps1 is modified \eval "$ask_conda" # escaping of backslash in prompt afterwards # 1. removes modifier again PS1=${PS1#*) } # 2. escapes backslashes in modifier CONDA_PROMPT_MODIFIER=$(echo "$CONDA_PROMPT_MODIFIER" | sed 's/\\/\\\\/g') # 3. prepends modifier again PS1="$CONDA_PROMPT_MODIFIER$PS1" if [ $rc != 0 ]; then \export PATH fi __conda_hashr }
  5. 将此行添加到您的C:\msys64\home\USERNAME\.bashrc中只有一个.condarc:
  6. export CONDARC=/c/Users/USERNAME/.condarc
  7. 将其添加到C:\Users\USERNAME\.condarc中,因此环境将放入默认路径,并且不会在MSYS2- $HOME目录和Windows %userprofile%目录中进行拆分:
  8. envs_dirs: - C:\Users\USERNAME\.conda\envs_dirs
  9. 将其附加到C:\msys64\home\USERNAME\.bashrc的末尾,因此您有了conda提示符,因此交互式python提示符有效:
  10. # >>> conda initialize >>> # only one .condarc -> the one in the windows user path # python call to interactive mode does not work by default, so wrapper python () { if [ $# -eq 0 ]; then command python -i else command python $@ fi } # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/c/miniconda3/Scripts/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/c/miniconda3/etc/profile.d/conda.sh" ]; then . "/c/miniconda3/etc/profile.d/conda.sh" else export PATH="/c/miniconda3/Scripts:$PATH" fi fi unset __conda_setup # <<< conda initialize <<<

现在,当您启动任何MSYS2 MinTTY时,conda库也已加载,pythonconda照常工作。唯一要记住的是:使用activate并想传递到conda环境的绝对路径时,请使用Windows路径并将其放在双引号中,如下所示:

activate "C:\myCondaEnv"
© www.soinside.com 2019 - 2024. All rights reserved.