Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anaconda python not available from sudo

I installed Anaconda in a Google Cloud Compute environment and can use it successfully from the shell as a normal user:

curt@lamp-v5mi:~$ python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org

However, when I start an interpreter via sudo python, anaconda is not the interpreter used, and I would like it to be.

curt@lamp-v5mi:~$ sudo python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Confusingly, when I start a shell as root and then start an interpreter, anaconda is the interpreter used.

curt@lamp-v5mi:~$ sudo -s
root@lamp-v5mi:/home/curt# python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org

I have export PATH="/anaconda/bin:$PATH" in both the root's and my normal account's .bashrc files. At first I thought the issue was sudo python not actually starting a root shell, and thus the export PATH="/anaconda/bin:$PATH" not actually being done. But when from my normal account I do sudo echo $PATH, it shows anaconda in there:

curt@lamp-v5mi:~$ sudo echo $PATH
/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

The anaconda installation was installed as root in /anaconda and I did a chmod -R 770 /anaconda to make it accessible to normal users, but I don't think this problem has anything to do with that.

How can I get anaconda to be the default interpreter when run from a sudo command line?

like image 716
Curt F. Avatar asked Jun 03 '15 16:06

Curt F.


People also ask

How do I enable python in Anaconda?

If you receive this warning, you need to activate your environment. To do so on Windows, run: c:\Anaconda3\Scripts\activate base in Anaconda Prompt.

Do I need admin rights to install Anaconda?

On Windows, macOS, and Linux, it is best to install Anaconda for the local user, which does not require administrator permissions and is the most robust type of installation. However, with administrator permissions, you can install Anaconda system wide.

Can I install Anaconda in root?

Getting Anaconda Or download the latest version of Anaconda and run the following command to install Python 3.5 (or 3.6) in the root environment: conda install python=3.5 or conda install python=3.6 . Or download the most recent Anaconda installer that included Python 3.5 (Anaconda 4.2. 0) or Python 3.6 (Anaconda 5.2.

How do I run a sudo command in Python?

Python code to execute command as a sudo user over ssh connection on a remote server using "paramiko" module. On The code snippet establishes connection and executes the command and return the status and output of the executed command. def get_ssh_connection(self, ssh_machine, ssh_username, ssh_password):


1 Answers

You almost got everything right. The only error is sudo echo $PATH, where $PATH is substitued BEFORE being sent to sudo, so it's your user PATH not your "sudoed" PATH that's displayed.

Note that your sudo implementation and configuration may change the PATH variable, as I can read in a "man sudo" (found from the Internet as I don't have sudo):

PATH
    May be overridden by the security policy.

So, in your "sudoed" PATH, there's probably no /anaconda/bin/

You may test this using sudo env | grep PATH.

To allow or change the PATH environment variable in your sudoed environment, I only can direct you to your man sudo, again: I'm not a sudo user.

The only thing I can drop you is sudo $(which python), as in sudo echo $PATH, the $(which python) will be executed by your user, resulting in /anaconda/bin/python, so actually running sudo /anaconda/bin/python which is another "solution".

To conclude, I should warn you that you probably don't want to run Python as root, there is almost no valid reason to do this, so your question is probably an XY problem: You got a first problem, you concluded by yourself it can be resolved by running Python as root, you tried sudo, got hit by sudo changing your PATH, then posted your "2nd level" problem here. What is your "real problem" ? The one that triggered the "Hum, I should try with sudo" ?

like image 73
Julien Palard Avatar answered Oct 14 '22 13:10

Julien Palard