Python Upgrading leads to terminal... not opening - python

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.

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 install sqlite3 for python3.7 in seperate directory on linux without sudo commands?

I have the problem that when I run my code on a linux server I get:
ModuleNotFoundError: No module named '_sqlite3'
So after researching, I found out sqlite3 was supposed to have been installed when I installed python, however it didn't.
I think the problem comes from the way I installed python. Since I do not have sudo permissions, I installed python3.7 in a local directory using: This guide.
All solutions to this sqlite3 problem that I can find requires sudo commands.
Is there another way that I can install python3.7 together with sqlite3 in my local Linux directory without using any sudo commands?
I hope I have stated my question clearly and I would appreciate all the help I can get. Thank you!
While installing a python package in a Linux system without "sudo" privileges you can use
For Python 3
pip3 install --user pysqlite3
You can install any third party packages with the same method
pip3 install --user PACKAGE_NAME
The --user flag to pip install tells Pip to install packages in some specific directories within your home directory. For more information click here.
Hope it helps !
The solution is to first build sqlite3 into a user directory and then build python using that directory's libraries and include headers. In particular, #Ski has answered a similar question regarding python 2, which can be adopted to python 3:
$ mkdir -p ~/applications/src
$ cd ~/applications/src
$ # Download and build sqlite 3 (you might want to get a newer version)
$ wget http://www.sqlite.org/sqlite-autoconf-3070900.tar.gz
$ tar xvvf sqlite-autoconf-3070900.tar.gz
$ cd sqlite-autoconf-3070900
$ ./configure --prefix=~/applications
$ make
$ make install
$ # Now download and build python 2, same works for python 3
$ cd ~/applications/src
$ wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz
$ tar xvvf Python-2.5.2.tgz
$ cd Python-2.5.2
$ ./configure --prefix=~/applications
$ make
$ make install
$ ~/applications/bin/python
Alternatively, if you already have to specify a different --prefix for some reason (this has happened to me with pyenv), use LDFLAGS and CPPFLAGS when configuring python build:
$ ./configure LDFLAGS=-L/home/user/applications/lib/ CPPFLAGS=-I/home/user/applications/include/

Upgrading python 3.5 to python 3.6 - Ubuntu

This is the first time I am working with python.
I am using ubuntu 16.04 system.
I am trying to change default python 3.5 to python 3.6
I am following this link to achieve this.
But I am getting the following error when I fire the command :
sudo update-alternatives --config python3
Error : bash: /usr/bin/python3: Too many levels of symbolic links
Please refer the screenshot of the steps which I followed and the commands that are executed.
I have first fired update and upgrade command.
I am not able to understand root cause of the issue, since I am new to python and its environment.
Thank you
follow below steps:-
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6
Following command worked for me:
sudo apt-get -f upgrade python3.6
Answers here seem a bit out of date, mainly because the PPA no longer exists. This is what worked for me (relying on one of the links posted in some of the question comments, among others):
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
(now we have both versions, need to switch the "toggle")
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 0
Mind the trailing 0.

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!

Categories

Resources