Pyenv-virtualenv: pyenv commands work differently when I execute them in shell and in shell script

Created on 11 Mar 2017  Â·  2Comments  Â·  Source: pyenv/pyenv-virtualenv

hi yyuu, I encountered a wired problem, I googled a lot relevant pages, but still got no solution. can you give me some advice, appreciate a lot for your effort。
I write a shell script, these command surely worked out differently when I execute by a shell script and execute separately.

here is my pyenv versions
[cj@model01 pyenv]$ pyenv versions
system
2.7.10
2.7.10/envs/cp.1
* anaconda-4.0.0 (set by /data1/cj/workspace/pyenv/version)
anaconda-4.0.0/envs/env.cj
cp.1
env.cj

and here is my shell script to create a virtualenv:
[cj@model01 pyenv]$ cat install_pyenv.sh
#!/bin/bash
# 设置虚拟环境
source ~/.bash_profile
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
PYTHON_VERSION="anaconda-4.0.0"
ENV_VERSION="env.cj"
pyenv install -v $PYTHON_VERSION -s
pyenv virtualenv $PYTHON_VERSION $ENV_VERSION
pyenv activate $ENV_VERSION

then I run the script, it says as follow:
[cj@model01 pyenv]$ /bin/bash install_pyenv.sh
pyenv-virtualenv: `/data1/cj/workspace/pyenv/versions/env.cj' already exists.

and now my pyenv versions is :
[cj@model01 pyenv]$ pyenv versions
system
2.7.10
2.7.10/envs/cp.1
* anaconda-4.0.0 (set by /data1/cj/workspace/pyenv/version)
anaconda-4.0.0/envs/env.cj
cp.1
env.cj

then I run the code in script seperately, I succeed
[cj@model01 pyenv]$ source ~/.bash_profile
[cj@model01 pyenv]$ export PYENV_VIRTUALENV_DISABLE_PROMPT=1
[cj@model01 pyenv]$ PYTHON_VERSION="anaconda-4.0.0"
[cj@model01 pyenv]$ ENV_VERSION="env.cj"
[cj@model01 pyenv]$ pyenv install -v $PYTHON_VERSION -s
[cj@model01 pyenv]$ pyenv virtualenv $PYTHON_VERSION $ENV_VERSION
pyenv-virtualenv: `/data1/cj/workspace/pyenv/versions/env.cj' already exists.
[cj@model01 pyenv]$ pyenv activate $ENV_VERSION
[cj@model01 pyenv]$ pyenv versions
system
2.7.10
2.7.10/envs/cp.1
anaconda-4.0.0
anaconda-4.0.0/envs/env.cj
cp.1
* env.cj (set by PYENV_VERSION environment variable)

Most helpful comment

I also sought to run some python from a shell script that tried to set the virtualenv, it failed similarly. I found that I needed to do the following first (as is done in one's shell init):

eval "$(pyenv init -)"

All 2 comments

I also sought to run some python from a shell script that tried to set the virtualenv, it failed similarly. I found that I needed to do the following first (as is done in one's shell init):

eval "$(pyenv init -)"

The activate command uses the PYENV_VERSION environment variable to set the version. That variable is set only as long as your script is running. The environment doesn't persist into the shell that started the script. If you want that, you should source your script. See below:

test$ pyenv local core
test$ pyenv version
core (set by /Users/hchapman/test/.python-version)
test$ cat > test.sh
#!/bin/bash

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
pyenv activate p
pyenv version
test$ bash test.sh
p (set by PYENV_VERSION environment variable)
test$ pyenv version
core (set by /Users/hchapman/test/.python-version)
test$
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tjorriemorrie picture Tjorriemorrie  Â·  8Comments

YumaInaura picture YumaInaura  Â·  3Comments

marshallpierce picture marshallpierce  Â·  7Comments

melentye picture melentye  Â·  7Comments

puhitaku picture puhitaku  Â·  5Comments