【发布时间】:2019-09-21 21:20:53
【问题描述】:
我正在使用 pyenv 来管理 python 安装和虚拟环境——我希望 anaconda 成为这样的安装之一,并且能够使用 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
(不知道为什么python版本只有安装anaconda才会添加到命令提示符)
当我创建一个新的虚拟环境 (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版本无法访问所有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.
...
问题在于,据我了解,每当我想要 anaconda 时以这种方式激活 python 版本意味着我只能访问 anaconda python 的一个版本 - 违背了在虚拟环境中使用它的目的。
还有一个难题 - 我注意到对于我的其他虚拟环境,~/.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? 中的 if 语句是否重要
更新 2:
问题解决了,我必须在新环境中安装python。
【问题讨论】: