autoenv icon indicating copy to clipboard operation
autoenv copied to clipboard

Implement autoenv unload

Open turicas opened this issue 13 years ago • 22 comments

In general I use autoenv to automatically activate my Python virtualenvs. It works pretty nice, but I miss a feature to automatically deactivate the virtualenv. It should deactivate the virtualenv if I change the directory to a path that is not inside the path which enabled the virtualenv. For example:

mkdir spam
echo "workon spam" > spam/.env
echo "deactivate" > spam/.env-unload
cd spam # run `source .env`, activating virtualenv 'spam'

# working in this directory...
mkdir eggs
cd eggs
touch ham
cd ..
echo "finished" > README.markdown
# finished working.

cd .. # runs `source .env-unload`, deactivating virtualenv

Another approach is to define bash functions inside .env (like autoenv_load and autoenv_unload) and call these functions instead of just sourcing .env/.env-unload.

turicas avatar Aug 11 '12 04:08 turicas

Yeah, I've considered this for a while, but decided it's out of scope (for now).

Thanks for the suggestions though, I'll definitely consider them in the future.

kennethreitz avatar Aug 11 '12 04:08 kennethreitz

I'll leave this issue open for discussion.

kennethreitz avatar Aug 14 '12 18:08 kennethreitz

I like the idea.

honzajavorek avatar Aug 15 '12 12:08 honzajavorek

+1

JeffPaine avatar Nov 16 '12 16:11 JeffPaine

I'd love if I can put it in .env with separate parts. Like

[load]
...
...
...

[unload]
...
...

Bankq avatar Nov 17 '12 17:11 Bankq

+1. I do see how it qualifies as out of scope, but I'd use an add-on (or completely separate) tool for handling unloading, if it existed.

bahoo avatar Nov 24 '12 01:11 bahoo

Simple workaround: place this .env somewhere high in your directory tree (possibly in the root)

if [ -n "$VIRTUAL_ENV" ] ; then
    deactivate
fi

I am sure there are some horrible side effects I am not taking into consideration, but it works for me. <_<

kazagistar avatar Oct 22 '13 19:10 kazagistar

I do hope you change you mind about this issue, but cool project anyway.

laithalissa avatar Nov 12 '13 20:11 laithalissa

Awesome project. I would love to see this one feature added as well.

bryanjos avatar Nov 27 '13 16:11 bryanjos

FYI: smartcd supports this.

blueyed avatar Sep 30 '14 18:09 blueyed

+1 for the unload feature.

wangyandongnx avatar Oct 28 '14 10:10 wangyandongnx

+1

msjaiswal avatar Oct 20 '15 07:10 msjaiswal

FYI: zsh-autoenv also supports this, either through a separate or the same file (where you can look at a variable to see which event it is).

blueyed avatar Oct 20 '15 22:10 blueyed

+1

unforswearing avatar Jan 18 '16 22:01 unforswearing

+1

kevinpostal avatar Feb 24 '16 20:02 kevinpostal

+1

Atry avatar May 05 '16 06:05 Atry

+1

luizs81 avatar Aug 09 '16 04:08 luizs81

+1

aliavni avatar Dec 26 '16 18:12 aliavni

I would suggest documenting the workaround by @kazagistar above somewhere in README. It could be useful for many people

SashaOv avatar Dec 26 '16 19:12 SashaOv

+1

yuanbohan avatar Nov 10 '17 02:11 yuanbohan

Hi, I'm running into the same issue and found the suggestion by @kazagistar to be very useful. Thanks.

One minor issue of that snippet is it will always run whenever I enter the subfolders, which will unnecessarily deactivate and reactivate the env. My two cents on the improvement:

recent_env=$(pwd)
while [ "${recent_env}" != "$HOME" ];do
    if [ -f ${recent_env}/.env ];then
        return
    fi
    recent_env=$(dirname "${recent_env}")
done
source deactivate

This will make sure the source deactivate runs only when there are no other .env files along the path except the one on the $HOME.

wddabc avatar Feb 12 '18 18:02 wddabc

I recommend just using direnv for this functionality.

kennethreitz avatar Feb 13 '18 19:02 kennethreitz

Closed as this was implemented in v0.3.0. Sorry for the pings - I know this is an older thread. If you are still using autoenv, free make a new issue for bug reports / unexpected functionality.

hyperupcall avatar Oct 14 '23 03:10 hyperupcall