我正试图通过bash脚本激活我的conda env.即使脚本运行正常并且我的PATH似乎在脚本中被更改,它也会在脚本终止后以某种方式重置.我可以source activate test
从cmd线调用,它工作正常.下面的输出示例.
脚本:
PycharmProjects/test » cat ./example.sh
echo "before calling source: $PATH"
source activate test
echo "after calling source: $PATH"
输出:
./example.sh before calling source: /Use rs/me/miniconda3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin discarding /Users/me/miniconda3/bin from PATH prepending /Users/me/miniconda3/envs/test/bin to PATH after calling source: /Users/me/miniconda3/envs/test/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin`
但如果我echo $PATH
在脚本完成后,你可以看到它$PATH
没有改变(即没有/Users/me/miniconda3/envs/test/bin
):
PycharmProjects/test » echo $PATH /Users/me/miniconda3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
在较新版本的conda(4.6+)上,我注意到以下作品:
eval "$(conda shell.bash hook)" conda activate
我发现以下内容适用于运行bash shell的Mac OSX:
#!/bin/bash source /Users/yourname/anaconda/bin/activate your_env python --version # example way to see that your virtual env loaded as expected
确保使用以下内容编写脚本化可执行文件:
chmod +x yourscript.bash