pyenv-virtualenv icon indicating copy to clipboard operation
pyenv-virtualenv copied to clipboard

pyenv commands work differently when I execute them in shell and in shell script

Open coderwhisky opened this issue 7 years ago • 2 comments

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)

coderwhisky avatar Mar 11 '17 07:03 coderwhisky

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 -)"

itegebo avatar May 05 '18 18:05 itegebo

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$

sr105 avatar Sep 24 '18 20:09 sr105

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 -)"

You are a lifesaver!

mrlonis avatar Mar 02 '23 05:03 mrlonis