开发者

Execute a script when moving to it's dir?

I'm playing around with virtualenv and pip, but I find it quite restrictive to have to "source bin/activate" each time I come into a virtualenv dir. So I'd like to automate it. Any ideas of a way to e开发者_StackOverflowxecute a script once we change to it's dir, or a shell features enabling this behavior?


Maybe you're looking for something like this in your .bash_profile::

has_virtualenv() {
    if [ -e .venv ]; then
        workon `cat .venv`
    fi
}
venv_cd () {
    cd "$@" && has_virtualenv
}
alias cd="venv_cd"

It replaces default cd command with a script which 1) does the actual cd, 2) checks for .venv file inside new directory, 3) if it's found, assumes that the file contains virtual environment name and executes workon `cat .venv`.

workon command is a replacement for plain source bin/activate; it's provided by virtualenvwrapper and has some niceties like postactivate hook. See project page for more details.

That piece of code above I found via post by Justin Lilly. See also tips in virtualenvwrapper's docs for some other cool stuff.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜