Build environment with pyenv, miniconda & conda-forge

2024-03-02
2022-08-16

When I tried to run a python in the environment using pyenv and miniconda, it didn't work because it pointed to the wrong package.

Installation

pyenv

terminal

$ brew update
$ brew install pyenv

The environment variables are probably added automatically when installing with Homebrew.

miniconda

terminal

$ pyenv install miniconda3-latest
$ pyenv global miniconda3-latest

After installing, it can switch the version of Python.

terminal

$ pyenv versions
  system
* miniconda3-latest

If the version doesn't change, the PATH of Miniconda may not be set correctly.

Settings

Ininialize and restart the shell.

terminal

$ conda init zsh
$ exec $SHELL

To use conda-forge in Miniconda, delete the default channel.

terminal

$ conda config --remove channels defaults
$ conda config --show channels
channels: []

The new channel can be added with the following command:

terminal

$ conda config --add channels conda-forge

Build the virtual environment

terminal

$ conda create -n myenv python=3.9 -c conda-forge --override-channels

Install Python when building an environment. If it is not installed, the error below may occur.

ModuleNotFoundError: No module named 'xxx'

terminal

$ conda activate myenv
$ python
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 17:01:00)
[Clang 13.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
 '/Users/[user]/.pyenv/versions/miniconda3-latest/envs/myenv/lib/python39.zip',
 '/Users/[user]/.pyenv/versions/miniconda3-latest/envs/myenv/lib/python3.9',
 '/Users/[user]/.pyenv/versions/miniconda3-latest/envs/myenv/lib/python3.9/lib-dynload',
 '/Users/[user]/.pyenv/versions/miniconda3-latest/envs/myenv/lib/python3.9/site-packages']

If the virtual environment under Miniconda is added to the path as shown above, there is no problem.