<p>快速解决<code>bash</code>:<strong>将下面的init脚本前置到Bash脚本中。</p>
<pre class="lang-sh prettyprint-override"><code>eval "$(command conda 'shell.bash' 'hook' 2> /dev/null)"
</code></pre>
<p>完成。</p>
<hr/>
<p>对于其他shell,请检查shell的init conf,在shell conf中复制以下内容并将其预处理到脚本中。</p>
<pre class="lang-sh prettyprint-override"><code># >>> conda initialize >>>
...
# <<< conda initialize <<<
</code></pre>
<p>你也可以使用</p>
<pre class="lang-sh prettyprint-override"><code>conda init --all --dry-run --verbose
</code></pre>
<p>获取脚本中所需的init脚本。</p>
<h3>解释</h3>
<p>这与在<code>conda</code>4.6中引入<code>conda init</code>有关。</p>
<p><a href="https://www.anaconda.com/conda-4-6-release/" rel="nofollow noreferrer">Quote from conda 4.6 release log</a></p>
<blockquote>
<p>Conda 4.4 allowed “conda activate envname”. The problem was that setting up your shell to use this new feature was not always straightforward. Conda 4.6 adds extensive initialization support so that more shells than ever before can use the new “conda activate” command. For more information, read the output from “conda init –help” </p>
</blockquote>
<p>在<code>conda</code>4.6中引入<code>conda init</code>之后,conda only expose命令
<code>conda</code>进入<code>PATH</code>但不是“base”中的所有二进制文件。所有平台上的<code>conda activate env-name</code>和<code>conda deactivate</code>统一了环境切换。</p>
<p>但是要使这些新命令工作,必须使用<code>conda init</code>进行额外的初始化。</p>
<p>问题是脚本文件在子shell中运行,而<code>conda</code>在此子shell中未初始化。</p>
<h3>参考文献</h3>
<ul>
<li><a href="https://www.anaconda.com/conda-4-6-release/" rel="nofollow noreferrer">Conda 4.6 Release</a></li>
<li><a href="https://github.com/pyenv/pyenv/wiki/Unix-shell-initialization" rel="nofollow noreferrer">Unix shell initialization</a></li>
<li><a href="https://blog.flowblok.id.au/2013-02/shell-startup-scripts.html" rel="nofollow noreferrer">Shell startup scripts</a></li>
</ul>