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?
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" ?
Related
This question already has answers here:
How to set Python3.5.2 as default Python version on CentOS?
(6 answers)
Closed 3 years ago.
When I run python on RHEL, I automatically use 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.
>>>
When I sudo python, it defaults to 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" gives:
/opt/anaconda3/bin/python
/bin/python
How can I make sudo commands run Anaconda distribution of python. Is there any risk of changing this?
Can I make the change permanently, or do I just run python from sudo using the full Anaconda path?
Your python version and installation location for root is different. If you want to use /opt/anaconda3/bin/python , there are different ways:
You can add alias python="/opt/anaconda3/bin/python" to your .bashrcfile of root user and re login or source this .bashrc.
Other way is to use #!/opt/anaconda3/bin/python in your python code when you run it from root user so that is uses your anaconda distribution.
Point is, you have to use /opt/anaconda3/bin/python as your python binary.
You mayalso remove python2.7 from your root user and add /opt/anaconda3/bin/python in your PATH env variable.
Also, you can add /opt/anaconda3/bin/python in your PATH environment variable and use python3 instead of python from root user. or you can use /opt/anaconda3/bin/python instead of python
Make sure the permissions and owner ship of paths are good without conflicts among users.
I am a non-superuser of a linux machine.
Currently it has 2 versions of Python.
When I invoke standard python command it gave version 2.6
$ python
[neversaint#mach71 ~]$ python
Python 2.6.2 (r262:71600, Jan 28 2011, 13:47:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which python
/opt/somedir/bin/python
It's only when I invoke with python2.7 it gives the version 2.7
[neversaint#mach71 ~]$ python2.7
Python 2.7.6 (default, Nov 11 2013, 13:13:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ which phython2.7
/usr/bin/python2.7
My question is how can I set it such that whenever I call $ python it will give me version 2.7.
You can simlink it into some directory both accessible to your user and in your $PATH. For example if /home/<your-username>/local/bin is in your $PATH then you can do
ln -s /usr/bin/python2.7 /home/<your-username>/local/bin/python
In this example /home/<your-username>/local/bin should be in your path before /usr/bin. If there is no such entry in your $PATH you can add it there:
export PATH=$HOME/local/bin:$PATH
You can also add this line to .bashrc or similar to activate it on shell start.
Use a shell alias, alias python=/usr/bin/python2.7 and then python will execute the result of that alias.
in /usr/bin create symlink to python27 or whatever python version you have
sudo ln -s python2.7 python
I'm trying to use a newer Python (2.7.3) with an old CentOS.
I have a recipe to install python to a non-standard location:
./configure --prefix=#{install_path} --with-threads --enable-shared --with-zlib=/usr/include
make
make install
I set the PATH and LD_LIBRARY_PATH variables to find bin/python and the .so files using /etc/profile.d/. This seems to mostly work.
With a non-root user, I get the right Python:
[vagrant#localhost ~]$ python
Python 2.7.3 (default, Dec 24 2012, 15:18:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
With a root user, I get the right Python:
[vagrant#localhost ~]$ sudo su
[root#localhost vagrant]# python
Python 2.7.3 (default, Dec 24 2012, 15:18:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
However, the $LD_LIBRARY_PATH hack seems to be a little wonked when using sudo:
[vagrant#localhost ~]$ sudo python
python: error while loading shared libraries: libpython2.7.so.1.0:
cannot open shared object file: No such file or directory
Even though the variables look right:
[vagrant#localhost ~]$ sudo which python
/opt/Python-2.7.3/bin/python
Adding Defaults env_keep += "LD_LIBRARY_PATH" to /etc/sudoers does not work.
sudo -i python does work.
sudo -E python does not work.
I'm curious what I could do to get sudo to pick up the right Python without -i?
related:
sudo changes PATH - why?
https://stackoverflow.com/questions/12593336/odd-path-behaviour-on-centos-python
Thanks to this blog post. You can forego the use of $LD_LIBRARY_PATH by linking with LDFLAGS in configure. Where #{ldlibpath} is #{install_path}/lib:
./configure --prefix=#{install_path} --with-threads --enable-shared \
--with-zlib=/usr/include LDFLAGS="-Wl,-rpath #{ldlibpath}"
As noted in the blog post, you will need to mkdir this ldlibpath before running configure.
I am trying to install setuptools via an egg on my VPS. However, I keep getting an error that Python2.6 doesn't exist despite the fact that it is in $PATH:
[root#host install]# sudo sh setuptools-0.6c11-py2.6.egg
setuptools-0.6c11-py2.6.egg: line 3: exec: python2.6: not found
[root#host install]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/courier-imap/sbin:/usr/lib/courier-imap/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/wt_python/bin
[root#host install]# sudo /usr/local/wt_python/bin/python2.6
Python 2.6.5 (r265:79063, May 18 2010, 16:49:22)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root#host install]# sudo python2.6
Python 2.6.5 (r265:79063, May 18 2010, 16:49:22)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Any idea what could be wrong?
Thanks
1 ) sudo as root is redundant.
2 ) for a quick hack symlink your python install into a standardized path. I'm not 100% familiar with the setuptools installer, but it could spawn a subshell that would mess with environment variables. For that matter sudo depending on flags messes with environment variables.
3) If there isnt a reason why you specifically need a custom python build why not use the one in the repositories?
Hope one or a few of the ideas help.
Rob
When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc.
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
When just starting a python interpreter using python, it doesn't offer tab completion.
Can someone tell me what django is doing to give me an interactive console, or what I need to do to start an interactive console without a django app?
I may have found a way to do it.
Create a file .pythonrc
# ~/.pythonrc
# enable syntax completion
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
then in your .bashrc file, add
export PYTHONSTARTUP=~/.pythonrc
That seems to work.
I think django does something like https://docs.python.org/library/rlcompleter.html
If you want to have a really good interactive interpreter have a look at
IPython.
For the record, this is covered in the tutorial: http://docs.python.org/tutorial/interactive.html
I use ptpython - it is a wonderful tool autocomplete shell cmd.
Installing ptpython is very easy, use pip tool
pip install ptpython
and for django shell, you should import the django env, like this
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")
Trust me, this is the best way for you!!!
Fix for Windows 10 shell:
pip install pyreadline3 # previously, pyreadline but that package was abandoned
pip install ipython
It looks like python3 has it out-of box!
In Python3 this feature is enabled by default. My system didn't have the module readline installed. I am on Manjaro. I didn't face this tab completion issue on other linux distributions (elementary, ubuntu, mint).
After pip installing the module, while importing, it was throwing the following error-
ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory
To solve this, I ran-
cd /usr/lib
ln -s libncursesw.so libncursesw.so.5
This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.
Yes. It's built in to 3.6.
fernanr#gnuruwi ~ $ python3.6
Python 3.6.3 (default, Apr 10 2019, 14:37:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 318 possibilities? (y or n)
os.CLD_CONTINUED os.O_RDONLY os.ST_NOEXEC os.environ os.getpid( os.readlink( os.spawnvpe(
os.CLD_DUMPED os.O_RDWR os.ST_NOSUID os.environb os.getppid( os.readv( os.st
For older versions (2.x) above script works like charm :)
fernanr#crsatx4 ~ $ cat .bashrc | grep -i python
#Tab completion for python shell
export PYTHONSTARTUP=~/.pythonrc
fernanr#crsatx4 ~ $ . ~/.bashrc
fernanr#crsatx4 ~ $ echo $?
0
fernanr#crsatx4 ~ $ python2
Python 2.7.5 (default, Jun 11 2019, 14:33:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT os.O_WRONLY