Guido on python3 for beginners

Matt Wheeler m at funkyhat.org
Thu Feb 18 11:44:36 EST 2016


On Thu, 18 Feb 2016 11:07 Chris Angelico <rosuav at gmail.com> wrote:

> By the way... For bash users, adding this to .bashrc may make venvs a
> bit easier to keep straight:
>
> checkdir() {
>     [ -n "$VIRTUAL_ENV" ] && ! [[ `pwd` =~ `dirname $VIRTUAL_ENV`* ]]
> && echo Deactivating venv $VIRTUAL_ENV... && deactivate
>     [ -z "$VIRTUAL_ENV" -a -d env ] && echo Activating venv
> `pwd`/env... && source env/bin/activate
> }
> PROMPT_COMMAND=checkdir
>
> (I'm more fluent in Python than in bash, so this is a bit ugly.)
>
> As soon as you change out of the directory that contains your venv,
> it'll be deactivated, so you can't accidentally run stuff in the
> "wrong" environment. And as soon as you change into a directory that
> contains an 'env' subdir, it'll activate it. (Also applies on first
> creation; as soon as "python3 -m venv env" returns, this will activate
> the env.)
>

This is a bad idea for the same reason enabling all magic comments in your
editor is a bad idea.
(rvm does this but confirms that the rvmrc it's about to activate is one
that you've previously approved, I wouldn't want to try to write a shell
function that did that though :)).

>



More information about the Python-list mailing list