virtualenvwrapper hooks aren't loading automatically

643 views Asked by At

(Following Marina Mele's taskbuster Django tutorial)

The virtualenv is called tb_test. In $VIRTUAL_ENV/bin/ (~/.virtualenvs/tb_test) I pasted the postactivate file from the general hooks folder in $WORKON_HOME and added 2 lines so now it looks like this:

$VIRTUAL_ENV/bin/postactivate #!/bin/zsh # This hook is sourced after every virtualenv is activated.

echo "hELo"
export DJANGO_SETTINGS_MODULE="taskbuster.settings.testing"

However, upon running workon tb_test the virtualenv gets activated and everything but the postactivate hook is not being loaded. No "heLo" echo :-(

2

There are 2 answers

0
villancikos On

I know it's been a while since this question was posted but now with Mac OS Catalina in more devices, ZSH comes as the default shell.

Well, I was having the same issue, couldn't activate environment variables.

What did the work was following the tips here: VirtualEnvWrapper Tips

In there, one of the tips suggest to add a rehash command after doing echo $variables. and did the trick.

Here are my postactivate or preactivate files in case:

### ~/.virtualenvs/MY_ENV/bin/post(pre)activate ###  
echo 'export DJANGO_SETTINGS_MODULE=MY_SETTINGS' >> ~/.zshenv
echo 'export SECRET_KEY=MY_SECRET' >> ~/.zshenv
echo rehash

Same for the postdeactivate and or predeactivate

It took me a while to find this tip, so hopefully this helps another helpless developer.

6
René Fleschenberg On

Works for me. Double check that you are using workon to activate the virtualenv (not source bin/activate), and that you are really activating the right virtualenv.

If it still does not work, please provide more information about your environment (OS, versions of involved packages, etc.). Did you modify any of the other virtualenv-wrapper hooks?

Update: Maybe you created the hook in the wrong path? If you create the virtualenv with mkvirtualenv, it should create the postactivate file for you in the right place, so you just have to edit it. It should be in this location:

~/.virtualenvs/tb_test/bin/postactivate

To clarify: not in ~/.virtualenvs/tb_test/postactivate