簡體   English   中英

用pyenv安裝anaconda,無法配置虛擬環境

[英]Installing anaconda with pyenv, unable to configure virtual environment

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. 使用pyenv install anaconda3-2019.03成功安裝,我可以使用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版本)

當我創建一個新的虛擬環境( pyenv virtualenv anaconda3-2019.03 datsci )時,它似乎按預期工作:

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 which python返回的相對位置相同,在我的其他虛擬環境中 python 都可以正常工作。

問題是在虛擬環境中運行的 python 版本無法訪問所有 anaconda 的包:

(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 安裝它確實如此:

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:

(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.

...

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 ,其中包含例如以下內容:

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

~/.pyenv/versions/datsci中沒有這樣的文件,所以我添加了一個:

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

無濟於事。

FWIW該目錄的內容是:

(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$ 

我想知道我的pyvenv.cfg文件中的 datsci 內容是否有誤……但我不知所措。

我真的很喜歡 pyenv 的工作方式,我想繼續使用它。 有沒有辦法配置 anaconda 在 pyenv 下正常運行?

更新:

我卸載了我的 pyenv 管理的 anaconda 並按照 Simba 的建議進行了全新安裝。 現在默認情況下 pyenv 確定活動的 python 安裝,我可以使用conda activate base激活 conda:

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 環境似乎無法正確激活:

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?

更新 2:

問題解決了,我必須在新環境中安裝 python。

個人建議:不要使用pyenv安裝Anaconda或者Miniconda。

pyenvconda都能夠管理不同的 python 環境。 anaconda安裝的pyenv應該只用作 Python 解釋器 從 pyenv 安裝的pyenv創建 Python 環境仍然由pyenv virtualenv處理,但不是conda env create

我一直在同時使用這兩個工具。 事實證明,最好的解決方案是分別安裝condapyenv ,並分別管理它們的虛擬環境。

  1. 總是初始化pyenv
  2. 只公開命令conda但不激活任何環境

細節

安裝pyenv

正常安裝 Anaconda 或 Miniconda,而不是通過pyenv install

確保pyenvconda命令在交互式 shell 中可用。

通過將以下內容放入pyenv初始化文件( .zshrc.bashrc ,ZSH 為 .zshrc)來初始化 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 中執行以下命令。

# 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設置的。 使用pyenvconda分別管理環境。

管理虛擬環境的示例。

# 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 conda info檢查 output 。

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM