【问题标题】:Python 2.7, not Anaconda 3 running when I run as root using sudo [duplicate]Python 2.7,而不是当我使用 sudo 以 root 身份运行时运行的 Anaconda 3 [重复]
【发布时间】:2019-11-03 02:02:51
【问题描述】:

当我在 RHEL 上运行 python 时,我会自动使用 Anaconda3:

Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

当我 sudo python 时,它默认为 python 2.7。

Python 2.7.5 (default, Sep 12 2018, 05:31:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

“which python” vs “sudo which python”给出:

/opt/anaconda3/bin/python

/bin/python

如何让 sudo 命令运行 Python 的 Anaconda 发行版。有没有改变这种情况的风险?

我可以永久进行更改,还是只使用完整的 Anaconda 路径从 sudo 运行 python?

【问题讨论】:

标签: python linux


【解决方案1】:

您的 python 版本和 root 的安装位置不同。如果你想使用 /opt/anaconda3/bin/python ,有不同的方法: 您可以添加alias python="/opt/anaconda3/bin/python" to your .bashrcroot 用户的文件并重新登录或获取此.bashrc

另一种方法是在你的 python 代码中使用#!/opt/anaconda3/bin/python,当你从 root 用户运行它时,它会使用你的 anaconda 发行版。

重点是,您必须使用/opt/anaconda3/bin/python 作为您的python 二进制文件。

您也可以从您的 root 用户中删除 python2.7,并在您的 PATH 环境变量中添加 /opt/anaconda3/bin/python

此外,您可以在 PATH 环境变量中添加 /opt/anaconda3/bin/python,并使用 python3 而不是 root 用户的 python。或者你可以使用/opt/anaconda3/bin/python 而不是python

确保路径的权限和所有权良好,用户之间没有冲突。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 2019-05-17
    • 1970-01-01
    • 2022-12-03
    • 2020-06-21
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多