简体   繁体   中英

sudo python wrong version

I get python 2.7 when running sudo. I've tried changing my .bash_profile and .bashrc from my original user and root, but to no avail.

$ 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.
>>> 

I've alias python='python3' in both bash configs in both users and sourced them.

How do I get python 3.5 to load with sudo python ?

EDIT: This is on OSX EDIT2: I've also tried looking at which python.

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

the two different pythons must be running from different locations on your computer. from the terminal, try running

whereis python and sudo whereis python . whereis will tell you what path you are running python from. then, once you figure out where 3.5 is you can run sudo <path to 3.5>

As a possible workaround you can add to your bash_aliases (or similar to it):

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

And then:

. .bashrc

Find the location of python for the root user. In the shell type sudo -i to change to root user. Then type which python .

Changing the python version of the system is not recommended. Some system scripts may face issues for this. You can set alias in the root users bashrc or similar files which should be located in /etc (eg /etc/bashrc ) directory.

I am not in mac and also I haven't tried this. So the answer is not tested.

sudo doesn't source your (or root's) .bashrc , and overrides much of the environment of the calling user, so adding aliases there isn't helpful.

Why not just type sudo python3 ?

Depending on your security settings, sudo -E python may work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM