我很少使用Anaconda来构建跨平台程序包,但我不想将其用作python的日常驱动程序。 我为Anaconda,Ruby和Node项目采用的是使用环境沙盒,这实际上将功能隐藏在功能背后,远离您的路径,直到您特别需要它为止。 我首先从这两个github回购中学到了这一点:
[HTTPS://GitHub.com/Ben van/sandbox的]
[HTTPS://GitHub.com/Maxim把这/dot files]
我有一个沙箱功能文件,如下所示:
.zsh / sandboxd.zsh:
#!/bin/zsh
# Based on
# https://github.com/maximbaz/dotfiles/.zsh/sandboxd.zsh
# which was originally adapted from:
# https://github.vom/benvan/sandboxd
# Start with an empty list of all sandbox cmd:hook pairs
sandbox_hooks=()
# deletes all hooks associated with cmd
function sandbox_delete_hooks() {
local cmd=$1
for i in "${sandbox_hooks[@]}";
do
if [[ $i == "${cmd}:"* ]]; then
local hook=$(echo $i | sed "s/.*://")
unset -f "$hook"
fi
done
}
# prepares environment and removes hooks
function sandbox() {
local cmd=$1
# NOTE: Use original grep, because aliased grep is using color
if [[ "$(type $cmd | \grep -o function)" = "function" ]]; then
(>&2 echo "Lazy-loading '$cmd' for the first time...")
sandbox_delete_hooks $cmd
sandbox_init_$cmd
else
(>&2 echo "sandbox '$cmd' not found.\nIs 'sandbox_init_$cmd() { ... }' defined and 'sandbox_hook $cmd $cmd' called?")
return 1
fi
}
function sandbox_hook() {
local cmd=$1
local hook=$2
#echo "Creating hook ($2) for cmd ($1)"
sandbox_hooks+=("${cmd}:${hook}")
eval "$hook(){ sandbox $cmd; $hook \$@ }"
}
.zshrc
在.zshrc中,创建沙盒函数:
sandbox_hook conda conda
此命令将正常的python可执行文件转换为:
conda () {
sandbox conda
conda $@
}
使用此技术的另一个好处是,由于采购大量包装器脚本(例如python、rvm等)会缩短外壳程序的启动时间,因此可以加快外壳程序的加载时间。
这也使我感到困惑,Anaconda默认将其python3可执行文件安装为python,这破坏了许多旧版python脚本,但这是一个单独的问题。 像这样使用沙箱使我明确意识到我正在使用Anaconda的python而不是系统默认值。