Bash配置 更好使用Python虚拟环境

在使用python虚拟环境virtualenv的时候,一般会在项目的同一级或则项目内部新建一个虚拟环境文件夹virtualenv,但是在每次进行开发时候都需要手动激活虚拟环境,为了每次操作时自动激活虚拟环境,只需要使用Bash覆盖cd即可实现

.bashrc 增加配置如下:

cd() { __override_cd cd "$@" ; }

__override_cd()
{
  typeset __zsh_like_cd_hook
  if
    builtin "$@"
  then
    shift || true # remove the called method
    if [[ -f .env ]]
    then
      source  .env
    fi
    true
  else
    return $?
  fi
}

cd .

上面的命令判断进入的目录有没有文件.env,如果有则尝试source

.env 文件内容如下:

source  ../virtualenv/bin/activate

我每次都是将虚拟环境文件夹放在项目同级virtualenv,可根据自己情况修改。

你可能感兴趣的:(Bash配置 更好使用Python虚拟环境)