sudo python版本错误

5

当我使用sudo运行时,我得到的是Python 2.7。我尝试从我的原始用户和root更改我的.bash_profile和.bashrc文件,但都没有成功。

$ python
Python 3.5.2 (default, Oct 11 2016, 05:05:28) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
$ sudo python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我已经在两个用户的bash配置文件中使用了alias python='python3',并且已经将它们引入。

如何让Python 3.5在sudo python命令下加载?

编辑:这是在OSX上 编辑2:我也尝试过查看which python。

$ which python
/usr/bin/python
$ sudo which python
/usr/bin/python

1
尝试运行命令 "env | grep PATH" 和 "sudo env | grep PATH",查看输出结果。 - Tyler
4个回答

2

两个不同的Python必须在您计算机上的不同位置运行。从终端中尝试运行:

whereis pythonsudo whereis python。whereis会告诉您正在从哪个路径运行Python。然后,一旦您找出3.5的位置,您就可以运行sudo <path to 3.5>


2

作为一种可能的解决方法,您可以添加到bash_aliases(或类似文件)中:

alias python='/usr/bin/python3'
alias sudo='sudo '

而后:
. .bashrc

0

查找root用户的Python位置。在shell中输入sudo -i以切换到root用户,然后输入which python

不建议更改系统的Python版本。这可能会导致某些系统脚本出现问题。您可以在root用户的bashrc或类似文件中设置别名,这些文件应位于/etc(例如/etc/bashrc)目录中。

我不在Mac上,也没有尝试过这个。因此,答案未经测试。


0

sudo 命令不会读取你(或 root 用户)的 .bashrc 文件,并且会覆盖调用用户的大部分环境变量,因此在该文件中添加别名是无效的。

为什么不直接输入 sudo python3 呢?

根据你的安全设置,sudo -E python 也可能有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接