autoenv
autoenv copied to clipboard
Implement autoenv unload
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.
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.
I'll leave this issue open for discussion.
I like the idea.
+1
I'd love if I can put it in .env with separate parts. Like
[load]
...
...
...
[unload]
...
...
+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.
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. <_<
I do hope you change you mind about this issue, but cool project anyway.
Awesome project. I would love to see this one feature added as well.
FYI: smartcd supports this.
+1 for the unload feature.
+1
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).
+1
+1
+1
+1
+1
I would suggest documenting the workaround by @kazagistar above somewhere in README. It could be useful for many people
+1
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.
I recommend just using direnv for this functionality.
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.