【问题标题】:Command 'python' not found after creating conda environment创建 conda 环境后找不到命令“python”
【发布时间】:2020-04-08 06:45:55
【问题描述】:

我在笔记本电脑上运行 Ubuntu 19.10,我最近安装了 miniconda3。到目前为止没有问题,但是在使用 python 2.7 创建环境后,每次打开终端时都会收到以下消息:

Command 'python' not found, but can be installed with:
sudo apt install python3         # version 3.7.5-1, or
sudo apt install python          # version 2.7.17-1
sudo apt install python-minimal  # version 2.7.17-1

所以我按照建议安装了python,然后出现了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named conda

我的猜测是 bash 正在尝试访问 conda python 2.7? 这是我在 conda 不活动的情况下运行 which python 得到的结果:

which python

/usr/bin/python

which python3

/usr/bin/python3

whereis python

python: /usr/bin/python2.7 /usr/bin/python3.7 /usr/bin/python3.7m /usr/bin/python /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.7 /etc/python2.7 /etc/python3.7 /etc/python /usr/local/lib/python2.7 /usr/local/lib/python3.7 /usr/include/python3.7m /usr/share/python /usr/share/man/man1/python.1.gz

这里激活 conda 是对应的结果:

which python

/home/rustax/miniconda3/bin/python

which python3

/home/rustax/miniconda3/bin/python3

whereis python

python: /usr/bin/python2.7 /usr/bin/python3.7 /usr/bin/python3.7m /usr/bin/python /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.7 /etc/python2.7 /etc/python3.7 /etc/python /usr/local/lib/python2.7 /usr/local/lib/python3.7 /usr/include/python3.7m /usr/share/python /home/rustax/miniconda3/bin/python3.7-config /home/rustax/miniconda3/bin/python3.7m /home/rustax/miniconda3/bin/python3.7 /home/rustax/miniconda3/bin/python3.7m-config /home/rustax/miniconda3/bin/python /usr/share/man/man1/python.1.gz

我尝试创建一个从 /home/rustax/miniconda3/bin/python 到 /usr/bin/python 的符号链接,但出现了同样的错误。

Conda 将以下行添加到 .bashrc:

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

提前感谢您提供的任何帮助

【问题讨论】:

  • 你说这个错误是在你新建conda环境后发生的。你是如何创建这个环境的?
  • conda create -n myenv python=2 但我查了一下,它不仅是在创建环境之后。更新新的 conda 安装后也会发生这种情况。我使用以下命令进行了更新: conda update --all
  • 请编辑您的问题以显示您为创建环境运行的命令、您如何激活环境以及您如何尝试运行 Python。
  • 抱歉,我已经解决了这个问题。 :P 很简单的一个 tbh。

标签: python ubuntu anaconda conda miniconda


【解决方案1】:

抱歉,我已经解决了这个问题。由于 Ubuntu 19.10 没有预装 python2,我为解决这个问题所做的是首先删除 python(因为我在安装 conda 后安装了它,请注意这是 python 而不是 python3,因为 python3 是预装的)和 conda 完全使用以下几行。

sudo apt-get purge --auto-remove python

rm -rf ~/miniconda3

rm -rf ~/.condarc ~/.conda ~/.continuum

之后我安装了python

sudo apt install python

在安装 python 之后,我以正常方式安装了 conda,现在我在打开终端时没有收到任何错误消息。我猜 PATH 变量的顺序很重要。自从我遇到麻烦时,我首先安装了 conda,然后安装了 python,并认为 condas python 路径可能是第一位的。

【讨论】:

  • 听起来您可能刚刚编辑了 .bashrc.bash_profile 以确保 Conda 路径最后添加到 PATH。虽然,通常 Conda 不能很好地配合 PATH 上的其他解释器,特别是如果它们与您环境中的 Python 的次要版本相同。
  • 我确实将 .bashrc 中的 PATH 从 export PATH="/home/rustax/miniconda3/bin:$PATH" 更改为导出 PATH="$PATH:/home/rustax/miniconda3/bin" ,但不确定这是否正确。但这没有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2022-06-10
  • 2017-04-02
相关资源
最近更新 更多