pyenv-virtualenv: Can not access Anaconda/Miniconda environment as virtualenv

I’m showing you the steps I use to create a virtualenv based on miniconda. Finally you will see that the virtualenv was created but I can’t access to it.

1.0. Check my initial pyenv versions:

$ pyenv versions
* system (set by /home/oscar/.python-version)
2.7.9
3.5.1

1.1 Install miniconda:

$ pyenv install miniconda3-4.0.5

$ pyenv versions
* system (set by /home/oscar/.python-version)
  2.7.9
  3.5.1
  miniconda3-4.0.5

1.2. Create the venvtest virtualenv based on miniconda:

$ pyenv virtualenv miniconda3-4.0.5 venvtest

$ pyenv versions
* system (set by /home/oscar/.python-version)
  2.7.9
  3.5.1
  miniconda3-4.0.5
  miniconda3-4.0.5/envs/venvtest
  venvtest

When I want to check if the virtualenv was created, It doesn’t show it. So I can’t no access to it 😢

$ pyenv virtualenvs
  miniconda3-4.0.5 (created from /home/oscar/.pyenv/versions/miniconda3-4.0.5)

$ pyenv activate ... (autocompleting)
--help            miniconda3-4.0.5  --unset 

Please, I hope you can help me.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 30

Most upvoted comments

Just to confirm to anyone visiting this thread. @taschini’s solution:

alexp@iMac (main@Py3.6.4):~$ pyenv virtualenv miniconda3-4.3.30 test
...
alexp@iMac (main@Py3.6.4):~$ pyenv versions
  system
  3.6.4
  3.6.4/envs/main
  data8
* main (set by /Users/alexp/.python-version)
  miniconda3-4.3.30
  miniconda3-4.3.30/envs/test
  test
alexp@iMac (main@Py3.6.4):~$ pyenv activate test
pyenv-virtualenv: version `test' is not a virtualenv
alexp@iMac (main@Py3.6.4):~$ ln -s /usr/local/var/pyenv/versions/miniconda3-4.3.30/bin/conda /usr/local/var/pyenv/versions/test/bin/
alexp@iMac (main@Py3.6.4):~$ pyenv activate test
pyenv-virtualenv: version `test' is not a virtualenv
alexp@iMac (main@Py3.6.4):~$ ln -s /usr/local/var/pyenv/versions/miniconda3-4.3.30/bin/activate /usr/local/var/pyenv/versions/test/bin/
alexp@iMac (main@Py3.6.4):~$ pyenv activate test
alexp@iMac (test@PyInc.):~$ pyenv deactivate test
alexp@iMac (main@Py3.6.4):~$ 

It seems that both conda and activate are required.

I created a pyenv-virtualenv hook for this:

alexp@iMac (main@Py3.6.4):~$ pyenv hooks virtualenv
/usr/local/var/pyenv/pyenv.d/virtualenv/venv_hook.bash
# /usr/local/var/pyenv/pyenv.d/virtualenv/venv_hook.bash

after_virtualenv "BASE_VERSION=\"$(echo $VIRTUALENV_NAME | awk -F/ '{print $(NF-2)}')\""
after_virtualenv "VENVNAME=\"$(echo $VIRTUALENV_NAME | awk -F/ '{print $(NF)}')\""
after_virtualenv 'CONDA=${PYENV_ROOT}/versions/${BASE_VERSION}/bin/conda'
after_virtualenv 'ACTIVATE=${PYENV_ROOT}/versions/${BASE_VERSION}/bin/activate'

after_virtualenv 'if [[ $BASE_VERSION = *"conda"* ]]; then echo "...linking conda and activate"; fi'
after_virtualenv 'if [[ $BASE_VERSION = *"conda"* ]]; then ln -s ${CONDA} ${PYENV_ROOT}/versions/${VENVNAME}/bin/; fi'
after_virtualenv 'if [[ $BASE_VERSION = *"conda"* ]]; then ln -s ${ACTIVATE} ${PYENV_ROOT}/versions/${VENVNAME}/bin/; fi'

After the hook is installed:

alexp@iMac (main@Py3.6.4):~$ pyenv install miniconda3-4.3.30 
Installing Miniconda3-4.3.30.1-MacOSX-x86_64...
...
Installed Miniconda3-4.3.30.1-MacOSX-x86_64 to /usr/local/var/pyenv/versions/miniconda3-4.3.30

alexp@iMac (main@Py3.6.4):~$ pyenv versions
  system
  3.6.4
  3.6.4/envs/main
  data8
* main (set by /Users/alexp/.python-version)
  miniconda3-4.3.30
alexp@iMac (main@Py3.6.4):~$ pyenv virtualenv miniconda3-4.3.30 test
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate test
#
# To deactivate an active environment, use:
# > source deactivate
#

Requirement already satisfied: setuptools in /usr/local/var/pyenv/versions/miniconda3-4.3.30/envs/test/lib/python3.6/site-packages
Requirement already satisfied: pip in /usr/local/var/pyenv/versions/miniconda3-4.3.30/envs/test/lib/python3.6/site-packages
...linking conda and activate
alexp@iMac (main@Py3.6.4):~$ pyenv activate test
alexp@iMac (test@PyInc.):~$ pyenv which python
/usr/local/var/pyenv/versions/test/bin/python
alexp@iMac (test@PyInc.):~$ pyenv deactivate test
alexp@iMac (main@Py3.6.4):~$ 

For me, on conda 4.1.2 I had to run source ~/.pyenv/versions/<env_dir>/bin/activate <env_name> at least once to make it work

Creating by hand the symbolic links within the environment bin directory to the conda and activate that you find in the conda base environment seems to fix the issue.

E.g.,

ln -s  ~/.pyenv/versions/miniconda3-latest/bin/activate ~/.pyenv/versions/miniconda3-latest/envs/test/bin/
ln -s  ~/.pyenv/versions/miniconda3-latest/bin/conda ~/.pyenv/versions/miniconda3-latest/envs/test/bin/

Edit This work-around is no longer necessary after #290.

I’m using pyenv 2.0.0 on macOS 10.14.6. pyenv local still does not work for me out-of-box. To be exact, both conda and activate commands are not in the bin directory in env created by miniconda.

@taschini 's answer is a workaround, not a solution. This is something pyenv should do internally, so this issue should be reopened until that’s the case.

@clbarnes I agree that pyenv should provide a built-in solution for this and this thread should remain open to continue tracking the issue.

#251 fixed activating an environment via conda:

pyenv install miniconda3-latest
pyenv activate miniconda3-latest
conda create --name myenv python
conda activate myenv

But I agree that doing this via pyenv-virtualenv should also work. (Though the above workflow is sufficient for me so I probably won’t put in a PR).

conda 4.x switched to using conda activate env-name