使用pyenv安装anaconda,无法配置虚拟环境

使用pyenv安装anaconda,无法配置虚拟环境

问题描述:

我正在使用pyenv来管理python安装和虚拟环境-我希望anaconda成为这样的安装之一,并希望能够使用anaconda python创建虚拟环境.使用 pyenv install anaconda3-2019.03 成功安装,我可以使用 pyenv global anaconda3-2019.03 激活版本:

I am using pyenv to manage python installations and virtual environments - and I would like anaconda to be one such installation, and to be able to create virtual environments using anaconda python. Using pyenv install anaconda3-2019.03 successfully installs and I can activate the version with pyenv global anaconda3-2019.03:

SamLee-PC:~ max$ pyenv versions
  system
  2.7.10
  2.7.10/envs/flask_tutorial
* 3.7.4 (set by /Users/max/.pyenv/version)
  3.7.4/envs/learning_python
  3.7.4/envs/microblog
  3.7.4/envs/stocktool
  3.7.4/envs/test1
  anaconda3-2019.03
  anaconda3-2019.03/envs/datsci
  datsci
  flask_tutorial
  learning_python
  microblog
  stocktool
  test1
SamLee-PC:~ max$ pyenv which python
/Users/max/.pyenv/versions/3.7.4/bin/python
SamLee-PC:~ max$ pyenv global anaconda3-2019.03
(anaconda3-2019.03) SamLee-PC:~ max$ pyenv which python
/Users/max/.pyenv/versions/anaconda3-2019.03/bin/python
(anaconda3-2019.03) SamLee-PC:~ max$ pyenv which conda
/Users/max/.pyenv/versions/anaconda3-2019.03/bin/conda

(我不知道为什么只有在安装anaconda的情况下才将python版本添加到命令提示符中)

(I do not know why the python version is added to the command prompt only with the anaconda installation)

当我创建一个新的虚拟环境( pyenv virtualenv anaconda3-2019.03 datsci )时,它似乎可以正常工作:

When I creat a new virtual environment (pyenv virtualenv anaconda3-2019.03 datsci), it appears to work as intended:

SamLee-PC:~ max$ pyenv which python
/Users/max/.pyenv/versions/3.7.4/bin/python
SamLee-PC:~ max$ cd code/linkedin/datsci
(datsci) SamLee-PC:datsci max$ pyenv which python
/Users/max/.pyenv/versions/datsci/bin/python
(datsci) SamLee-PC:datsci max$ 

这是 pyenv返回的相对位置,该位置在我的其他虚拟环境中都正常运行.

This is the same relative location returned returned by pyenv which python in my other virtual environments, which all work properly.

问题是在虚拟环境中运行的python版本无权访问anaconda的所有软件包:

The problem is that the python version that gets run in the virtual environment doesn't have access to all of anaconda's packages:

(datsci) SamLee-PC:datsci max$ anaconda-navigator
pyenv: anaconda-navigator: command not found

The `anaconda-navigator' command exists in these Python versions:
  anaconda3-2019.03

(datsci) SamLee-PC:datsci max$ 

尽管如果我手动激活python安装,它也会这样做:

though it does if I activate the python installation manually:

SamLee-PC:~ max$ pyenv global anaconda3-2019.03
(anaconda3-2019.03) SamLee-PC:~ max$ anaconda-navigator
WARNING: The conda.compat module is deprecated and will be removed in a future release.
/Users/max/.pyenv/versions/anaconda3-2019.03/lib/python3.7/site-packages/anaconda_navigator/api/conda_api.py:1364: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  data = yaml.load(f)
2019-09-21 14:03:38,666 - ERROR download_api._download:234
Invalid url https://www.anaconda.com/wp-content/uploads/2017/05/Webinar20-20Three20Ways20to20Move20your20Data20Science20Projects20to20Production.png
#anaconda navigator GUI opens

(奇怪的是, datsci 指向 的python版本似乎可以识别conda:

(oddly, the python version that datsci points to does seem to recognize conda:

(datsci) SamLee-PC:datsci max$ pyenv which python
/Users/max/.pyenv/versions/datsci/bin/python
(datsci) SamLee-PC:datsci max$ conda
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

...

问题是,据我所知,每当我想要anaconda时都以这种方式激活python版本意味着我只能访问anaconda python的一个版本-违反了在虚拟环境中使用它的目的.

The problem is that, as I understand it, activating the python version this way whenever I want anaconda means that I only have access to one version of anaconda python - defeating the purpose of using it in a virtual environment.

另一个难题-我注意到,对于其他虚拟环境,〜/.pyenv/versions/name-of-virtualenv 包含文件 pyvenv.cfg ,其中包含例如以下:

One more piece of the puzzle - I noticed that for my other virtual environments, ~/.pyenv/versions/name-of-virtualenv contains a file pyvenv.cfg, which contains e.g. the following:

  1 home = /Users/max/.pyenv/versions/3.7.4/bin                                                                   
  2 include-system-site-packages = false
  3 version = 3.7.4

〜/.pyenv/versions/datsci 中没有这样的文件,所以我添加了一个:

There was no such file in ~/.pyenv/versions/datsci, so I added one:

  1 home = /Users/max/.pyenv/versions/anaconda3-2019.03/bin                                                       
  2 include-system-site-packages = false
  3 version = anaconda3-2019.03

无济于事.

FWIW该目录的内容为:

FWIW the contents of that directory are:

(anaconda3-2019.03) SamLee-PC:datsci max$ pwd
/Users/max/.pyenv/versions/datsci
(anaconda3-2019.03) SamLee-PC:datsci max$ ls
bin     conda-meta  include     lib     pyvenv.cfg  share       ssl
(anaconda3-2019.03) SamLee-PC:datsci max$ 

我想知道datsci的 pyvenv.cfg 文件中的内容是否错误...但是我很茫然.

I wonder if I have the wrong contents in the pyvenv.cfg file for datsci...but I am at a loss.

我真的很喜欢pyenv的工作方式,并且我想继续使用它.有没有一种方法可以将anaconda配置为在pyenv下正常运行?

I really like the way pyenv works, and I would like to continue using it. Is there a way to configure anaconda to behave properly under pyenv?

我卸载了由pyenv管理的anaconda,并按照Simba的建议进行了全新安装.现在默认情况下,pyenv确定活动的python安装,我可以使用 conda activate base 激活

I uninstalled my pyenv-managed anaconda and did a fresh install as per Simba's recommendation. Now by default pyenv determines the active python installation, and I can activate conda with conda activate base:

Maxs-MacBook-Air:~ max$ which python
/Users/max/.pyenv/shims/python
Maxs-MacBook-Air:~ max$ conda activate base
(base) Maxs-MacBook-Air:~ max$ which python
/Users/max/anaconda3/bin/python
(base) Maxs-MacBook-Air:~ max$ 

但是,在使用 conda create --name datsci 创建新的conda环境之后,新的conda环境似乎无法正常激活:

However after creating a new conda environment with conda create --name datsci, the new conda environment does not seem to activate properly:

Maxs-MacBook-Air:~ max$ conda activate datsci
(datsci) Maxs-MacBook-Air:~ max$ which python
/Users/max/.pyenv/shims/python

我想念什么?

这是我的 .bash_profile 的内容:

  1 export PATH="/Users/max/.pyenv/bin:$PATH"                                                                                                                                                                                                 
  2 eval "$(pyenv init -)"
  3 eval "$(pyenv virtualenv-init -)"
  4 
  5 # >>> conda initialize >>>
  6 # !! Contents within this block are managed by 'conda init' !!
  7 __conda_setup="$('/Users/max/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
  8 if [ $? -eq 0 ]; then
  9     eval "$__conda_setup"
 10 else
 11     if [ -f "/Users/max/anaconda3/etc/profile.d/conda.sh" ]; then
 12         . "/Users/max/anaconda3/etc/profile.d/conda.sh"
 13     else
 14         export PATH="/Users/max/anaconda3/bin:$PATH"
 15     fi
 16 fi
 17 unset __conda_setup
 18 # <<< conda initialize <<<

复制在Simba的 .bash_profile中找到的if语句是否重要?

问题解决了,我不得不在新环境中安装python.

Problem solved, I had to install python in the new environment.

个人建议:请勿使用 pyenv 安装Anaconda或Miniconda.

Personal recommendation: Don't use pyenv to install Anaconda or Miniconda.

pyenv conda 都能够管理不同的python环境.由 pyenv 安装的 anaconda 仅应用作Python解释器.从 pyenv 安装的anaconda创建Python环境仍由 pyenv virtualenv 处理,而不由 conda env create 处理.

Both pyenv and conda are able to manage different python environments. The anaconda installed by pyenv should only serves as a Python interpreter. Python environment creation from anaconda installed by pyenv is still handled by pyenv virtualenv but not conda env create.

我一直一起使用这两个工具.事实证明,最好的解决方案是分别安装 conda pyenv ,并分别管理其虚拟环境.

I've been using these two tools together. It turns out the best solution is to install conda, pyenv separately, and manage their virtual environments separately as well.

  1. 始终初始化 pyenv
  2. 仅公开命令 conda ,但不激活任何环境
  1. alway initialize pyenv
  2. only expose command conda but don't activate any environment

详细信息

安装 pyenv .

请正常安装Anaconda或Miniconda,而不要通过 pyenv install 安装.

Install Anaconda or Miniconda normally, NOT by pyenv install.

确保在交互式shell中可以使用 pyenv conda 命令.

Make sure the pyenv and conda commands are available in an interactive shell.

初始化 pyenv .

# Put the content into ~/.bashrc or ~/.bash_profile for Bash,
# .zshrc for ZSH

# you may need to add dir of command `pyenv` into PATH,
# if command pyenv is not available yet

if command -v pyenv &>/dev/null; then
    eval "$(pyenv init -)"
fi
if command -v pyenv-virtualenv &>/dev/null; then
    eval "$(pyenv virtualenv-init -)"
fi

暴露命令 conda ,但不激活任何环境,即使是 base 环境.在您的Shell中执行以下命令.

Expose command conda but don't activate any environment, even the base environment. Execute the following commands in your shell.

# Run the content in the shell

# 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

注意:完成此设置后,默认的python是 pyenv global 设置的python.使用 pyenv conda 分别管理环境.

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

管理虚拟环境的示例.

# 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 env create new-env python=3.6
conda env list
conda activate new-env
conda deactivate

pyenv 的默认环境位置是〜/.pyenv/versions .

conda 的默认环境位置,请检查 conda信息的输出.

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