How to revert back to python 2.7? - python

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

Related

How to set default Python version without --update-alternatives

I have Python3.6 and Python3.8 installed on Ubuntu 18.04. When I execute python3 -V I get python3.8.0, which is correct. That's currently my default Python.
I used the Ubuntu 18.04 package repository when I installed Python3.6 and Python3.8. I just built Python3.10 from source and I want to set that as the default now. The canonical way to change is with --update-alternatives, but I did not do that when I installed Python3.6 and Python3.8, so when I execute:
update-alternatives --query python
update-alternatives: error: no alternatives for python
Python3.8 is in /usr/bin/python3.8 and Python3.10 is in /usr/local/bin/python3.10.
According to what I have read on the net, I should do this:
$ sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.8
$ sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.10
Then I select the default with sudo update-alternatives --config python.
Because I currently don't have any alternatives but I was somehow able to switch from Python3.6 to Python3.8 (I don't remember how), will the commands above cause any problems? Is there a problem in the different locations for 3.8 (/usr/bin/) vs 3.10 (/usr/local/bin/) and what will be the effect of the symlink for 3.10 going into /usr/local/bin/ where it's already located.
Naturally I could just try it, but if there is an issue I'm not sure how to back out of it if it goes wrong. I've gotten a lot of differing information on the net.
Thanks for any help.
I have answered my own question. First, I did have alternatives, but as Ubuntu defaults to Python2 (please Ubuntu change that -- the world has moved on), the command is sudo update-alternatives --config python3:
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.8 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.8 2 manual mode
Note the line "providing /usr/bin/python3." That means the Python symlinks for Python 3 are in /usr/bin/python3. So I add the new one:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 3
That makes Python 3.10 priority 3, the highest priority. Then:
$ python3 --version
Python 3.10.0
I hope that helps someone in the future.

How to set Python 3.8 as my default python version on kali linux

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!

sudo: python: command not found

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

My python installation is broken/corrupted. How do I fix it?

I followed these instructions on my RedHat Linux version 7 server (which originally just had Python 2.6.x installed):
beginning of instructions
install build tools
sudo yum install make automake gcc gcc-c++ kernel-devel git-core -y
install python 2.7 and change default python symlink
sudo yum install python27-devel -y
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2.7 /usr/bin/python
yum still needs 2.6, so write it in and backup script
sudo cp /usr/bin/yum /usr/bin/_yum_before_27
sudo sed -i s/python/python2.6/g /usr/bin/yum
sudo sed -i s/python2.6/python2.6/g /usr/bin/yum
should display now 2.7.5 or later:
python -V
end of instructions
The above commands and comments were taken from:
http://www.lecloud.net/post/61401763496/install-update-to-python-2-7-and-latest-pip-on
The python -v command returned this:
-bash: python: command not found
Now it is as if I have no Python installed. I don't want yum to break. I tried installing Python 3.4.
whereis python shows this:
python: /usr/bin/python2.6 /usr/bin/python2.6-config /usr/bin/python /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python2.7 /usr/local/bin/python3.4m-config /usr/local/bin/python2.7-config /usr/local/bin/python3.4 /usr/local/bin/python3.4m /usr/local/lib/python2.7 /usr/local/lib/python3.4 /usr/include/python2.6 /usr/share/man/man1/python.1.gz
What should I do now? I want a working installation of Python. For certain things I'm doing, I need it to be 2.7 or higher. I want yum to still work.
Do
sudo update-alternatives --remove-all python
sudo ln -sf /usr/bin/python2.7 /usr/bin/python
I got the same issue while upgrading ubuntu 18 to 19, this made it:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2.7 /usr/bin/python
do-release-upgrade
From:
https://bugs.launchpad.net/ubuntu/+source/ubuntu-release-upgrader/+bug/1825655
For me, nothing worked except this one:
unlink /usr/bin/python3
ln -s /usr/bin/python3.7 /usr/bin/python3
Credit: https://josephgeis.dev/2020/04/upgrading-to-ubuntu-20-04-python3/
This is easily fixed by installing the python27 package via yum. It should install in /usr/bin, and may overwrite the /usr/bin/python symlink that should be pointing to 2.6. If it did (just run ls -l python* in /usr/bin to see), remove the symlink and point it back to 2.6. Next create a symlink for /usr/local/bin/python pointing at /usr/bin/python2.7. Finally, modify your ~/.bashrc or ~/.bash_profile (whichever you use) to have /usr/local/bin before /usr/bin in your PATH:
export PATH=/usr/local/bin:$PATH
at the very end of the file. This way, /usr/bin/python remains linked to Python 2.6, which is what the system expects, and when you run python at the command line it'll start up 2.7. You shouldn't have to make any changes to the yum script either - just blanket replacing python with python2.6 without understanding what you're doing is not a very good idea.
I'd also recommend installing Python 3.4 in /usr/local/bin if possible, where the binary will be named python3 by convention. Even if it installs in /usr/bin, you'll still have the choice of running python3 or python3.4 to specify which version you want. I work on a CentOS system that has each version of Python from 2.4 up to 3.4 installed, all in /usr/local/bin (I'm sure this was done manually, and not via yum), while the only python* in /usr/bin is 2.6. I couldn't find a python3 package for RedHat (I may not have been looking hard enough), so I'd recommend building the latest version from source (3.4.3 as of this writing). Unzip the tarball in a suitable directory, check out the README file, then, in the Python-3.4.3 directory, run ./configure --help to see what the options are, and if you need to change anything. As long as you have gcc installed, and don't need to link to any weird math libraries or anything, you should just be able to run:
./configure
make
make test
sudo make install
and it'll install to /usr/local/bin. Check the messages at the end of the make step, as it'll list any modules it wasn't able to build there. Fails usually happen because you don't have a required library installed, so look in setup.py in the base directory in the detect_modules() function (starting on line 449, and stretching all the way down to line 1564). Install both the lib and the -devel packages so you get the necessary headers.
This same process can also be followed if you want to install the latest 2.7.9, instead of RH's 2.7.5. One of the major (in my eyes) advantages of 2.7.9 is that pip is installed by default, making third-party module installation that much easier.
Good luck!

Changing default python to another version

Currently when I use "python" command, it points to python2.6. I have installed python3.1 and I want the "python" command point to python3.1. How it is possible?
mahmood#mpc:~$ which python
/usr/bin/python
mahmood#mpc:~$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 2010-11-24 16:14 /usr/bin/python -> python2.6
mahmood#mpc:~$ uname -a
Linux orca 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux
Since you have Linux, and if you want to simply type "python" instead of "python3" in order to run Python programs, a solution is simply to define an alias in you shell configuration file (.bashrc, etc.). For Bourne shells, it should be something like
alias python=python3
(or whatever your Python 3 name is).
This way, you do not have to change anything on your system, so this solution should quite innocuous and it should not break your system.
You really don't want to change what python points to, because some programs might expect Python 2, and break.
The solution is to use virtualenv: create an isolated Python 3 environment (with the -p python3 option), activate it, and you're good to go.
unlink /usr/bin/python
ln -s /usr/bin/python3.1 /usr/bin/python
It is not advisable.
You could write at the top in your own script (a shebang):
#!/usr/bin/env python3
If you're on Windows then install pylauncher. It understands #!.
On Linux to make your script executable, run once:
$ chmod +x your-script
After that, to run your script:
$ ./your-script
For interactive use you could create virtualenv as #Petr Viktorin points out. To install/upgrade (versions from Ubuntu's repositries are too old):
$ pip install -U virtualenv{,wrapper}
Follow instructions in /path/to/virtualenvwrapper.sh, to create virtualenv that uses python3:
$ mkvirtualenv --python python3 py3
To activate virtualenv:
$ workon py3
In an active virtualenv python refers to /path/virtualenv/bin/python. So you could run:
$ python your_module.py
You could follow this procedure:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.1 /usr/bin/python
But as already stated by Petr Viktorin, any programs that would expect python v2 would stop to work. So use with caution. You can undo the change by running:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python2.6 /usr/bin/python
On Linux/Mac OS you can use python3 instead of python.

Categories

Resources