I want to make sudo python find Python 3.
I had a strange issue where, in terminal, typing python --version gave 3.6 but sudo python --version gave 2.7. After trying a few things I finally uninstalled 2.7 with sudo apt-get purge python2*. That removed everything correctly. Still, I can't get sudo python to find Python 3.
I've tried changing my /root/.bashrc to have:
export PATH="/home/username/anaconda3/bin:$PATH"
and
alias python="/home/username/anaconda3/bin/python"
and I put the same lines in ~/.bashrc too.
My etc/sudoers has this line:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"/usr/bin:$
I've opened new terminals and even restarted the computer. Any ideas how to make sudo python just find Python 3? I don't want a single session fix but something that will work each time I use the terminal.
Thanks
Your /etc/sudoers is explicitly configured to override your user's path with a known, secure one.
That said, if you want to always path the user's PATH through, you can easily override sudo with a function that will do this (installed in your ~/.bashrc or similar to make it persistent):
psudo() { sudo env PATH="$PATH" "$#"; }
thereafter, psudo python will use the same python interpreter that would be found in the PATH.
If you really want to override the sudo command itself, that's doable too:
sudo() { command sudo env PATH="$PATH" "$#"; }
The command builtin prevents the function from recursing (calling itself).
If you don't want to modify your bashrc, you can always do this:
sudo env "PATH=$PATH" python something
...other approach.
when I got to this post, I was just looking to run:
python -m spylon_kernel install
as I ran the command above, I got a message telling me to use sudo in addition to what I was typing, such as
sudo python -m spylon_kernel install
as I did it, I got the 'sudo: python: command not found' message from console, and adding --user such as:
python -m spylon_kernel install --user
was simply enough to get it done.
Notice that I did not use sudo command within the last command.
The accepted answer suggests setting up functions to duplicate or replace sudo, with syntax new Linux user might find complex.
There is a simpler way...
User has miniconda3 python env:
(base) user#machine:~/$ which python
/home/user/miniconda3/bin/python
(base) user#machine:~/$ python --version
Python 3.9.12
sudo can not see python:
(base) user#machine:~/$ sudo which python
(base) user#machine:~/$ sudo python --version
sudo: python: command not found
Simply use "which python" in place of "python"!:
(base) user#machine:~/$ sudo `which python` --version
Python 3.9.12
This allows the shell interpreter to replace "python" with "/home/user/miniconda3/bin/python" in the sudo command.
Alternatively, set an environment variable, say PY to always use in place of python - this has the advantage of being usable inside shell scripts:
(base) user#machine:~/$ export PY=`which python`
(base) user#machine:~/$ $PY --version
Python 3.9.12
(base) user#machine:~/$ sudo $PY --version
Python 3.9.12
Note: sudo with --preserve-env=PATH is attractive, but does not work, because sudo uses secure_path from /etc/sudoers to look up executables, not $PATH.
If python 3.x is installed already, try the following code
sudo python3
Related
I just upgraded python in ubuntu to python 3.10 to use the match/case statements, but now, my terminal won't open. Anywhere. I tired opening it on Visual Studio Code, but it says the path does not exist. Gnome terminal and terminator won't even give any feedback. I am using Ubuntu 20.04 with i3wm, my shell is zsh, and here are the commands i used (i modified them from this site and this site):
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 10
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python2.6 10
sudo update-alternatives --config python
sudo update-alternatives --config python3
For what I remember my previous python version was somewhere around 3.7.
If you prefer, here are the specs from above:
OS: Ubuntu 20.04
Shell: zsh (oh-my-zsh)
WM: i3
Terminal: gnome-terminal
Important: Please remember I do not have access to a terminal. I have still not tried recovery mode, but if you have a non-termianl based solution, it is preferred.
Just found a way to undo it. If you encounter the same problem, here is what I did:
Ctrl+Alt+Fn and a few F buttons (F1, F2, F3...) until I got to a tty menu.
I logged in as myself (not root), and ran sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 to make the default python 2.8 with sudo update-alternatives --config python3 again. Then it worked properly.
Edited
New Answer
I found a better way to install your preferred python version. It's more stable and more secure than using ppa:deadsnakes/ppa repository.
Sure now this is not a direct answer to the problem you had anymore. But I am editing this answer here in hope to help you and anyone that may want to install python3.10 in a machine that does not come with it installed by default.
You can use asdf, which is a tool version manager. With it you may install any python version you want, independent of your OS. It is gonna be more verbose, but works perfectly well, without any conflict with some pre-installed python version.
To avoid problems when installing asdf and python, install the following packages:
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
Then run:
sudo apt update && sudo apt upgrade
to make sure everything is up to date.
Installing asdf
To install asdf, first we clone from the repository and the branch of asdf version we want (Please note that the version specified in the end of the command may change. I just followed the documentation):
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
Now you only got to add a few lines to the end of your script configuration file. It may change according to your shell, but again, check the documentation for specific instructions (including zsh).
To find out what kind of terminal you're using, you can run:
echo $SHELL
If your're using bash then you can add the following to the end of your ~/.bashrc (yes, the dots are included):
. $HOME/.asdf/asdf.sh
. $HOME/.asdf/completions/asdf.bash
After that, you just have to restart your terminal.
Installing python3.10 (or any version you want)
Now with asdf installed, you should install a plugin to manage multiple python versions. To do that, just run the command:
asdf plugin-add python
To globally install python3.10.4 in your system, run:
asdf install python 3.10.4
And then:
asdf global python 3.10.4
Lastly, run:
asdf reshim
This last command is a somewhat refresh for asdf to work properly after globally installing libraries. Run it whenever you install something with asdf, just to be sure.
No need to worry about running this command after installing some package or module with pip though (well, at least I never had).
Credits to Kenzie Academy. Learned it there.
Old Answer
I ran exactly into the same problem. I found the answer here.
At first, I tried to simply open the gnome-terminal file without a terminal and change it, but it didn't work, because using a GUI I had no sudo permissions. So the workaround that worked for me and I expect that may work for you, was to open the file with VSCode and then used the VSCode integrated terminal to run the following command:
sudo nano /usr/bin/gnome-terminal
Of course you don't have to use nano, you could use vim or something else. Anyway, once there, you can change the first line of the file, which is a comment, from
#!/usr/bin/python3
to
#!/usr/bin/python3.8
Ubuntu 20.04 is well compatible with python3.8, so that's why I suggested you putting python3.8 there but you could surely test if some other version works.
This is not a way to go back to python3.8. You will still have the add-ons of python3.10.
Whenever I ran python --version the result was 2.7 and 3.8 for python3 --version. I wanted to get 3.8 by running only python --version instead of python3 --version and after looking for the same on stackoverflow I did this
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
and while it did what I wanted but now a lot of things are going haywire. Therefore, I want my system to go back to the way it was before.
P.S. I wanted python to call python3 in the first place because whenever I would run mkvirtualenv name it would give me a warning that I am using python 2.7 and should switch to 3.8 but python 3.8 was already there on my computer and I thought maybe by doing the above mentioned I would get rid of the warning.
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2 /usr/bin/python
I installed python 3.8 in my kali linux OS, but when I type python in kali linux terminal it showing like this
> Python 2.7.18 (default, Apr 20 2020, 20:30:41)
How can I set Python 3.8 as my default?
Hope this answers your question.
The easiest way would be to run alias python=python3 .
So, now if you run the command python it will load python3 instead of python while being under the same alias/command python.
What I usually do is install non-default versions of programs under /usr/local. Then I configure the file $HOME/.bash_profile so that the $PATH variable has /usr/local/bin first, before /usr/bin and /bin (which might actually be the same physical directory on your system).
$HOME/.bash_profile:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# PATH starts with...
PATH="/usr/local/bin"
# add $HOME/bin if it exists...
if [ -d "${HOME}/bin" ]; then
PATH="${PATH}:${HOME}/bin"
fi
# These should exist, add them as fall back....
PATH="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin"
# Some custom locations last
if [ -d /usr/local/arm-elf/bin ]; then
PATH="${PATH}:/usr/local/arm-elf/bin"
fi
if [ -d /opt/local/bin ]; then
PATH="${PATH}:/opt/local/bin"
fi
This has the effect of making anything located in /usr/local/bin the default when I am using an interactive shell, and letting the system defaults remain in effect otherwise.
first, check if you have python3.8 installed
use
ls /usr/bin/python*
then check for an alternative python version using
update-alternatives --list python
if the output of python3.8 is set to 2
To set the default python command to run python3.8
use this to create a persistent symlink
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
the 2 depends on your python alternatives
then
python --version
First remove python 2.7.18
Type >> sudo apt purge -y python2-minimal
install python latest version
Type >> sudo apt install -y python3-pip
latest version of python is installed!
I've been keeping all my python2.7 installs in my ~/.local/ directory that way I don't have to sudo every time I want to do a pip install. I also have $HOME/.local/lib/python2.7/site-packages on my $PYTHONPATH. This has worked well for years but now I find myself needing to run python3 programs more frequently. After much research it seems like virtualenv is the most recommended way to deal with python 2 and 3 on the same system. But I am running into troubles. I can spin up a python3 virtual environment but when I try to install new libs with pip, my old global path (i.e. ~/.local/) is still being searched by pip, which makes sense. However, this is even the case if I remove my ~/.local/bin/ directory from my $PATH and unset my $PYTHONPATH.
Here is are the steps I took:
First check the preliminaries before activating virtualenv. (I'm on Ubuntu 16.04 btw)
maddoxw#firefly:~$ echo $PATH
/usr/local/cuda-8.0/bin:/home/maddoxw/.node_modules_global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/maddoxw/bin:/home/maddoxw/scripts
maddoxw#firefly:~$ echo $PYTHONPATH
maddoxw#firefly:~$ python --version
Python 2.7.12
maddoxw#firefly:~$ python3 --version
Python 3.5.2
maddoxw#firefly:~$ which pip
Since I removed my ~/.local/bin directory from my path, then I can be certain pip will not be found. Also, $PYTHONPATH is still empty. Now I create my virtualenv:
maddoxw#firefly:~$ mkdir test && cd test/
mkdir: created directory 'test'
maddoxw#firefly:~/test$ python3 -m venv .env
maddoxw#firefly:~/test$ source .env/bin/activate
(.env) maddoxw#firefly:~/test$ echo $PATH
/home/maddoxw/test/.env/bin:/usr/local/cuda-8.0/bin:/home/maddoxw/.node_modules_global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/maddoxw/bin:/home/maddoxw/scripts
(.env) maddoxw#firefly:~/test$ echo $PYTHONPATH
(.env) maddoxw#firefly:~/test$ which python
/home/maddoxw/test/.env/bin/python
(.env) maddoxw#firefly:~/test$ python --version
Python 3.5.2
(.env) maddoxw#firefly:~/test$ which pip
/home/maddoxw/test/.env/bin/pip
Good. My ~/.local/ is still NOT on my $PATH, $PYTHONPATH is still empty, python points to the correct path and version, and pip is pointing to the correct location. Now lets try to pip install a fresh lib.
(.env) maddoxw#firefly:~/test$ pip install Cython
Requirement already satisfied: Cython in /home/maddoxw/.local/lib/python2.7/site-packages
Why is pip still looking in a non-$PATH path?
First, install pip3 to use with python3. You can install it with the following command and then use pip3 to install your packages.
sudo apt-get install python3-pip
[Solved]
When I initially set up my python2.7 environment way back when, I created for myself a handy little function wrapper around pip so that I wouldn't have to type out the --user every time I wanted to pip install
pip() {
if [ "$1" = "install" -o "$1" = "bundle" ]; then
cmd="$1"
shift
$HOME/.local/bin/pip $cmd --user $#
else
$HOME/.local/bin/pip $#
fi
}
I put this function in ~/.bash.d/bash_functions
and in my ~/.bashrc i added the line,
[ -f ~/.bash.d/bash_functions ] && source ~/.bash.d/bash_functions
So, although I removed $HOME/.local/ from my path. This wrapper function was still being called everytime I fired up a new terminal. Weather or not I was or was not in a virtualenv was irrelevant.
Solution?
Commented out (or delete completely) the function wrapper fixed it.
I am using CentOS. The default python installed is 2.4 and I also installed 2.7 in order to use Django.
How can I configure Django to use /usr/local/bin/python2.7 instead of just the default python command?
I have to leave the default Python as 2.4 because other services such as yum don't run with 2.7.
Or is there other solution?
mod_wsgi is linked with specific version of Python. You have to recompile it with Python 2.7 and make Apache load new module (by editing file like /etc/apache2/mods-enabled/wsgi.load or /etc/apache2/modules.d/70_mod_wsgi.conf).
http://code.google.com/p/modwsgi/wiki/InstallationIssues
Just run this command into your command shell: python2.7 /path/to/manage.py runserver
Then Django will run under python 2.7
The answer is to use virtualenv to set up a virtual environment for python2.7 and to install your stuff into that sandbox.
Here is some code to get you going:
$ sudo apt-get install python-setuptools python-dev build-essential
$ sudo easy_install -U pip
$ sudo pip install virtualenv virtualenvwrapper
$ mkdir ~/.virtualenvs
$ sudo cat >> ~/.bashrc << EOF
# virtualenvwrapper setup
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages --python=python2.7'
source /usr/local/bin/virtualenvwrapper.sh
EOF
$ source ~/.bashrc
$ mkvirtualenv test
$ pip install django
I am assuming that CentOS uses sudo like Ubuntu does. Substitute the native call if not.