Using pyenv + pyenv-virtualenv + direnv to manage python projects.
Using Ansible to deploy the application in aws instance.
Configured the instance using steps mentioned in https://github.com/direnv/direnv/wiki/Python for pyenv & pyenv-virtualenv.
While using ansible in a direnv configured folder, it was not able to load .envrc file even though it is
present.
This is the output of direnv status before cd to the folder.
```
"direnv exec path /usr/bin/direnv",
"DIRENV_CONFIG /home/ubuntu/.config/direnv",
"DIRENV_BASH /bin/bash",
"No .envrc loaded",
"No .envrc found",
After cd, this is the output of `direnv status`
```
"direnv exec path /usr/bin/direnv",
"DIRENV_CONFIG /home/ubuntu/.config/direnv",
"DIRENV_BASH /bin/bash",
"No .envrc loaded",
"Found RC path /home/ubuntu/project_folder/.envrc",
"Found RC mtime 1532751850",
"Found RC allowed true",
"Found RC allowPath /home/ubuntu/.config/direnv/allow/d59aec829a40b2db3229075dfdddd6e51bda380ee3455f9cd693c3dd8252d863"
I have the following ansible task in my playbook.
- name: check python version
command: bash -lc "direnv status; cd /home/ubuntu/project_folder; direnv status; which python"
The above direnv status are output from this task. This always returns me root python and not the virtual env python.
But, If I chdir on Linux terminal on that aws instance, the .envrc gets loaded and so the python virtualenv.
Am I missing something ?
Try adding eval "$(direnv export bash)" after every time that the current directory is being changed.
In a normal shell this is executed on every prompt but since now prompt is being displayed by ansible this needs to be invoked by hand.
If there is just one command that needs to be run with the loaded context, it's also possible to use direnv exec <DIR> <COMMAND> ... which will load the .envrc for the target DIR before executing the command.
Most helpful comment
Try adding
eval "$(direnv export bash)"after every time that the current directory is being changed.In a normal shell this is executed on every prompt but since now prompt is being displayed by ansible this needs to be invoked by hand.
If there is just one command that needs to be run with the loaded context, it's also possible to use
direnv exec <DIR> <COMMAND> ...which will load the.envrcfor the target DIR before executing the command.