Conda build: ModuleNotFoundError: No module named 'conda'

I get an “ModuleNotFoundError: No module named ‘conda’” error when building a conda package through conda build.
Before the error triggers, conda-build actually does a lot of things successfully, like creating the package plans etc. Later in the process it dies when it wants to activate the build env.
I tried many things, like making sure I have only one env (so there is no python version mismatch), the paths are set correctly etc. Web-search does not give any other hints.

Below is the snippet of the log showing the error (using --debug). Any idea?

+++ xxxx/third_party/install/miniconda3/bin/python -m conda shell.bash hook
++ eval 'export CONDA_EXE='\''xxxx/third_party/install/miniconda3/bin/conda'\''
export _CE_M='\'''\''
export _CE_CONDA='\'''\''
export CONDA_PYTHON_EXE='\''xxxx/third_party/install/miniconda3/bin/python'\''

# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
__conda_exe() (
    "$CONDA_EXE" $_CE_M $_CE_CONDA "$@"

__conda_hashr() {
    if [ -n "${ZSH_VERSION:+x}" ]; then
    elif [ -n "${POSH_VERSION:+x}" ]; then
        :  # pass
        \hash -r

__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.
        \unset CONDA_PS1_BACKUP
    \local ask_conda
    ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix "$@")" || \return
    \eval "$ask_conda"

__conda_reactivate() {
    \local ask_conda
    ask_conda="$(PS1="${PS1:-}" __conda_exe shell.posix reactivate)" || \return
    \eval "$ask_conda"

conda() {
    \local cmd="${1-__missing__}"
    case "$cmd" in
            __conda_activate "$@"
            __conda_exe "$@" || \return
            __conda_exe "$@"

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}"}"
        PATH="$(\dirname "$(\dirname "$CONDA_EXE")")/condabin${PATH:+":${PATH}"}"
    \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

conda activate base'
+++ export CONDA_EXE=xxxx/third_party/install/miniconda3/bin/conda
+++ CONDA_EXE=xxxx/third_party/install/miniconda3/bin/conda
+++ export _CE_M=
+++ _CE_M=
+++ export _CE_CONDA=
+++ _CE_CONDA=
+++ export CONDA_PYTHON_EXE=xxxx/third_party/install/miniconda3/bin/python
+++ CONDA_PYTHON_EXE=xxxx/third_party/install/miniconda3/bin/python
+++ '[' -z '' ']'
+++ export CONDA_SHLVL=0
+++ '[' -n '' ']'
+++++ dirname xxxx/third_party/install/miniconda3/bin/conda
++++ dirname xxxx/third_party/install/miniconda3/bin
+++ PATH=xxxx/third_party/install/miniconda3/condabin:xxxx/third_party/install/miniconda3/conda-bld/sharpy_1709127147272/_build_env/bin:xxxx/third_party/install/miniconda3/conda-bld/sharpy_1709127147272/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/bin:xxxx/third_party/install/miniconda3/bin:xxxx/third_party/install/miniconda3/condabin:xxxx/third_party/install/miniconda3/condabin:xxxx/third_party/install/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
+++ export PATH
+++ '[' -z '' ']'
+++ PS1=
+++ conda activate base
+++ local cmd=activate
+++ case "$cmd" in
+++ __conda_activate activate base
+++ '[' -n '' ']'
+++ local ask_conda
++++ PS1=
++++ __conda_exe shell.posix activate base
++++ xxxx/third_party/install/miniconda3/bin/conda shell.posix activate base
Traceback (most recent call last):
  File "xxxx/third_party/install/miniconda3/bin/conda", line 12, in <module>
    from conda.cli import main
ModuleNotFoundError: No module named 'conda'
+++ ask_conda=
+++ return

This might be a bug somewhere in conda(-build).

I was given a hint and found a workaround:
The shebang in “conda” (from default channel and from conda-forge) is "#!/usr/bin/env python”. In theory this should work fine because I set up the PATH correctly. Something within the conda-build process seems to reset PATH so that most likely it uses the system python.
When I patch “conda” to use the concrete python of my miniconda install (sed -i "s,\#\!/usr/bin/env python,#\!$CONDA_ROOT/bin/python," $CONDA_ROOT/*bin/conda) the error no longer occurs and things work fine.