【问题标题】:Why am I stuck with the system default Python 2 interpreter after activating a Python 3 Conda environment?为什么我在激活 Python 3 Conda 环境后仍使用系统默认的 Python 2 解释器?
【发布时间】:2020-06-09 04:26:32
【问题描述】:

我在 x86-64 Linux 大型机上创建了一个新的 Conda 环境,使用命令

conda create --name myenv --file somefile.txt --python=3.8.

我在这个环境中使用conda list 仔细检查了我的 Python 版本,结果返回

...
python                    3.8.3                hcff3b4d_0
...

但是,激活此环境后,Python 3 脚本无法运行,运行which python 表明该环境默认使用默认系统 Python 2 解释器:

$ which python
/usr/bin/python

到目前为止我的努力

首先,我将export PATH=$PATH:/home/miniconda3/envs/myenv/bin 行添加到我的~/.bashrc~/.profile 文件中,但没有效果。 which python 仍然返回 /usr/bin/python

然后我检查了我的别名文件以查看 python 是否别名为 Python 2。但别名文件中没有关于 python 的条目。

供您参考

  1. 我的~/.bashrc 看起来像这样:
$ cat ~/.bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

alias julia="/home/julia-1.4.0/bin/julia"

export PATH=$PATH:/home/miniconda3/envs/myenv/bin
$ cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
$ cat ~/.profile
export PATH=$PATH:/home/miniconda3/envs/myenv/bin

【问题讨论】:

  • which python3 的输出是什么?
  • @jared_mamrot /usr/bin/python3,这不是我需要的
  • 是的 - 这表明你的 python 的 conda 版本需要移动到你的路径的前面,正如@donald_stierman 所说
  • 首先,我将行 export PATH=$PATH:/home/qingyanz/miniconda3/envs/myenv/bin 添加到我的 ~/.bashrc 和 ~/.profile 文件中,以 no效果。 IIRC Conda 文档建议您不要手动将其添加到 PATH 中。您是否执行了 Miniconda 的标准/正常安装?

标签: python linux anaconda


【解决方案1】:

我认为您需要将 env 路径添加到 PATH 变量的 front 中,以便它首先在那里找到 python。操作系统将在 PATH 列表中查找文件或应用程序,并使用它找到的第一个匹配项。

改变

导出 PATH=$PATH:/home/qingyanz/miniconda3/envs/myenv/bin

到这里:

导出 PATH=/home/qingyanz/miniconda3/envs/myenv/bin:$PATH

例如,以下是我的 conda 环境中的设置:

(ds_tensorflow) C:\Users\me>哪个python /c/Users/me/miniconda3/envs/ds_tensorflow/python

(ds_tensorflow) C:\Users\me>env |路径 ... PATH=/c/Users/me/miniconda3/envs/ds_tensorflow:/mingw-w64/bin:/usr/bin:/bin:...

【讨论】:

  • 谢谢!这行得通!我将同一行添加到我的.bashrc,但直到我对.profile 执行相同操作后它才起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2016-03-05
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多