Pyenv-virtualenv: "Failed to activate virtualenv" when running activate virtualenv from /bin/sh

Created on 24 Mar 2015  ยท  4Comments  ยท  Source: pyenv/pyenv-virtualenv

I need to be able to activate a virtualenv from pyenv-virtualenv from a script. It can't be a plugin, I just want to run it as a normal command, the same way workon and source ~/path/to/virtualenv/bin/activate does.

https://github.com/tony/tmuxp-config/blob/master/python/bootstrap_env.sh#L118

The script above is meant to abstract activating virtualenv projects. Inside the script pyenv virtualenvs works, sourcing a virtualenv outright works, running workon in virtualenv works, but pyenv activate <env> fails. Running pyenv activate hhi works perfect in the same shell.

$ ./bootstrap_env.sh -c "echo wha"  myvirtualenv                                  
detected manager pyenv-virtualenv

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

$ pyenv activate myvirtualenv                                                                       โŽ
(myvirtualenv)$

Most helpful comment

@tony: I'm having the same problem. Can you explain your solution in more detail and how it could be applied to other scripts?

All 4 comments

Closing this. The issue is if I want bootstrap_env.sh to source the new environment too, I had to do ./bootstrap_env.sh -c "echo wha" myvirtualenv.

@tony: I'm having the same problem. Can you explain your solution in more detail and how it could be applied to other scripts?

yes, I try to use direnv to enable automatically the appropriate virtualenv through a project directory.envrc and it fails. Reinvoking manually within the shell works.

$> cat myproject/.python-v* 
2.7.14
toto
$> cat myproject/.envrc
# (rootdir)/.envrc : direnv configuration file
# see https://direnv.net/
pyversion=$(head .python-version)
pvenv=$(head     .python-virtualenv)

if [ -n "$(which pyenv-virtualenv)" ]; then
  pyenv virtualenv --force --quiet ${pyversion} ${pvenv}-${pyversion}
  # activate it
  pyenv activate ${pvenv}-${pyversion}
fi 

Now if I cd into the directory (after allowing the .envrc):

$> cd myproject
direnv: loading .envrc
[...]
Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

Yet activating it manually (or by sourcing the file) works

$> pyenv activate toto-2.7.14

I have this same issue. It works fine manually but from a script, it fails everytime when I do a pyenv virtualenv and then pyenv activate

Was this page helpful?
0 / 5 - 0 ratings