How can I install Anaconda aside an existing pyenv installation on OSX?

Question:

Sincerest apologies if this is easily found elsewhere, but although I found a number of posts with pyenv and Anaconda explanations, none addressed this issue specifically. However, I am often an idiot.

On Mac OSX (Mojave 10.14.6) I installed pyenv via Homebrew

brew install pyenv

And I happily install and switch between Python versions with

pyenv install ...

and

pyenv global ...

I typically use VS Code as my IDE.

I now have need to do some work in Anaconda. I haven’t used it before. Can I simply install Anaconda via the distribution site and use its navigator, and when I need my old python versions use pyenv and VS Code, or will there be a conflict when I install Anaconda? If there would be a conflict, is there a path to running both on OSX?

I could install it and see what happens of course, and restore from backup if it’s a big mess. But I’m hoping that a pyenv / Anaconda guru might have some sage words of advice that would save me potentially hours of cleaning up.

Thanks in advance!

Asked By: Dribbler

||

Answers:

Not super familiar with conda but I do use pyenv a lot.

Pyenv has its own virtualenv manager that you can use. You can always check which virtualenv version is active with:

pyenv versions

You should see something like:

  system
  20190814_125309
* 3.7.4 (set by /home/tzhuang/.pyenv/version)
  3.7.4/envs/20190814_125309
  3.7.4/envs/buildmaster-sandbox
  3.7.4/envs/HEAD
  3.7.4/envs/myenv
  3.7.4/envs/sandbox
  buildmaster-sandbox
  HEAD
  myenv
  sandbox

Where the * indicates the currently active virtualenv (this can be set using pyenv global like you mentioned). You can manually activate any virtualenv with:

pyenv shell

Eg.

pyenv shell sandbox

Then running pyenv versions gives:

  system
  20190814_125309
  3.7.4 (set by /home/tzhuang/.pyenv/version)
  3.7.4/envs/20190814_125309
  3.7.4/envs/buildmaster-sandbox
  3.7.4/envs/HEAD
  3.7.4/envs/myenv
  3.7.4/envs/sandbox
  buildmaster-sandbox
  HEAD
  myenv
* sandbox

It’s generally a good idea to install any packages you want into a new virtualenv instead of the global virtualenv. It makes it easier to debug environment/dependency issues should you run into any.

Answered By: Kapitol

There is a conflict, cause both pyenv and conda try to expose a global Python environment by default.

I’ve been using these tools together and best solution found by me is to

  1. Alway initialize pyenv, use the Python set by pyenv global as the default Python
  2. Only expose command conda but do NOT activate any environment from it

Detail

Since pyenv has been installed on your machine, you only need to install Anaconda.

brew cask install anaconda

Init conda without exposing the “base” environment from conda.

# init conda, the following command write scripts into your shell init file automatically
conda init

# disable init of env "base"
conda config --set auto_activate_base false

Done.

Note: After this setup, the default Python is the one set by pyenv global. Use pyenv and conda to manage environments separately.

Examples of managing virtual environments.

# virtual environments from pyenv
pyenv install 3.6.9
pyenv virtualenv 3.6.9 new-env
pyenv activate new-env
pyenv deactive
# You can also use `pyenv local`


# virtual environments from conda
conda create -n new-env python=3.6
conda env list
conda activate new-env
conda deactivate

Default env location for pyenv is ~/.pyenv/versions.

Default env location for conda, check output from conda info.

Extended Readign

Answered By: Simba

There is a much simpler solution to the one mentioned by Simba.

You can use pyenv-virtualenv

It allows you to manage virtual environments (including conda ones) right from pyenv.

Here is an example scenario:

  • I have python 3.10.5 installed with pyenv and set as the global Python version.
  • I installed anaconda3-2022.05 with pyenv, and I want to use a conda environment in a project.
  • I installed pyenv-virtualenv with Homebrew (since I installed pyenv with Homebrew as well) and I placed its init in my .zshrc.
  • I created a virtual conda environment using pyenv: pyenv virtualenv anaconda3-2022.05 my-conda-env
  • I navigated to the project folder and: pyenv local my-conda-env
  • Now the .python-version file refers to my-conda-env and each time I navigate to that folder in the terminal my-conda-env will autoactivate, and will deactivate when I navigate out of the project folder (this is because we have the init of pyenv-virtualenv loaded).

P.s. in my experience you don’t need to place the line that auto disables anaconda base environment in your shell config file. The base environment didn’t auto activate for me anyways.

Note: also be mindful of the placements of the three additions to your shell config file: the pyenv one, the pyenv-virtualenv one, and the anaconda one. The one placed later in the file is the one that loads last and hence its adjustment to the PATH will take the most precedent.

Answered By: murtadha96

For those on Windows, the Conda installer automatically handles this for you. Just uncheck "Add Anaconda3 to my PATH environment variable," and uncheck "Register Anaconda3 as my default Python 3.9".

If you do that, you won’t have any conflicts with pyenv. Just open the conda terminal when you want to use conda and continue using your normal terminal as before.

Explanation:

  1. Choose whether to add Anaconda to your PATH environment variable or register Anaconda as your default Python. We don’t recommend adding Anaconda to your PATH environment variable, since this can interfere with other software. Unless you plan on installing and running multiple versions of Anaconda or multiple versions of Python, accept the default and leave this box checked. Instead, use Anaconda software by opening Anaconda Navigator or the Anaconda Prompt from the Start Menu. – Windows Install Instructions

screenshot of anaconda setup

Answered By: Preston Badeer