51

I just tried to install python 3.6 on my Ubuntu 16.04 system, and now I can't run the terminal from the launcher or from Ctrl + Alt + T. I tried to run gnome-terminal from XTerm and got the following message:

Traceback (most recent call last):
  File "/usr/bin/gnome-terminal", line 9, in <module>
    from gi.repository import GLib, Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was: Traceback (most recent call last): File "/usr/bin/gnome-terminal", line 9, in <module> from gi.repository import GLib, Gio File "/usr/lib/python3/dist-packages/gi/init.py", line 42, in <module> from . import _gi ImportError: cannot import name '_gi'

How can I fix this error?

It should also be noted that:

  • My gnome-terminal file is now a python script.
  • The gnome-terminal.real file will open the terminal as expected.
  • python3.5 gnome-terminal will open the terminal as expected.
Error404
  • 7,440
bengdahl
  • 613

7 Answers7

50

Probably the symlink /usr/bin/python3 points to python3.6, which it should not. Fix it by running these commands:

sudo rm /usr/bin/python3
sudo ln -s python3.5 /usr/bin/python3
Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
  • 5
    To add to this: this is caused by a bug in the python3-apt package; there's a Launchpad bug open about it here. – ash Feb 06 '17 at 14:31
  • The symlink should be created on which path ? Isn't pyenv a better alternative until the terminal bug is fixed ? – josircg Mar 17 '18 at 15:44
  • @josircg: Not sure I understand your path question. Both the symlink and the executable reside in /usr/bin. There may well be better alternatives. If you know of any, please write an own answer. – Gunnar Hjalmarsson Mar 17 '18 at 19:42
  • About the path: it think it's wise to add a cd /usr/bin before the ln, right? – josircg Mar 17 '18 at 20:04
  • About the solution: if you try to enable again python 3.6, you wil have to remove/recreate the symlink again. This should be noted too on the solution. – josircg Mar 17 '18 at 20:06
  • @josircg: cd'ing isn't necessary in this case. The result would be the same. Not sure I see the value with the note you suggest. The question was asked for an Ubuntu version (16.04) which is based on python3.5. The symlink shouldn't point to any other python version as long as you are on 16.04. If/when you upgrade it will be fixed automatically. – Gunnar Hjalmarsson Mar 17 '18 at 21:01
  • 1
    @GunnarHjalmarsson where can I run these commands, my terminal is not opening. – Shoyeb Sheikh Apr 09 '19 at 06:47
  • 1
    @ShoyebSheikh: Maybe you have XTerm? – Gunnar Hjalmarsson Apr 09 '19 at 15:32
  • @GunnarHjalmarsson I got it working thanks very much, https://askubuntu.com/questions/1132349/terminal-not-opening-up-after-upgrading-python-to-3-7 – Shoyeb Sheikh Apr 09 '19 at 15:52
30

You don't have to point Python3 to python3.5 , just running the commands:

cd /usr/lib/python3/dist-packages/gi/
sudo cp _gi.cpython-35m-x86_64-linux-gnu.so _gi.cpython-36m-x86_64-linux-gnu.so
sudo cp _gi_cairo.cpython-35m-x86_64-linux-gnu.so _gi_cairo.cpython-36m-x86_64-linux-gnu.so
wjandrea
  • 14,236
  • 4
  • 48
  • 98
Inno Jia
  • 417
  • 4
  • 2
12

Proper change of default python3 is done via:

sudo update-alternatives --config python3

Although copying the _gi_cairo.cpython-35m-x86_64-linux-gnu.so from python3.5 to python3.6 could resolve the issue, but would break your library consistency. And should be avoided.

Saeed
  • 371
8

I know this is an old question but for those looking for a way other than changing python3 to python 3.5 globally, you can change the first line of /usr/bin/gnome-terminal from #! /usr/bin/python3 to #! /usr/bin/python3.5.

Note: I don't have python 3.5 on my computer but import gi works in python 3.6, so used 3.6 instead of 3.5.

  • 5
    Is gnome-terminal the only thing that breaks or are there other gnome-* things that I should change too? –  Oct 18 '19 at 00:14
4

REAL REASON + SOLUTION - Python3.7 Gnome Terminal Gets Messed Up - Ubuntu Linux Mint

THIS QUESTION WAS NEVER ANSWERED in another post here Terminal not opening up after upgrading python to 3.7 so I give the cause, reason and solution.

Install python 3.7, correct move, but missing PyGObject gi causes Gnome Terminal to fail on boot, fix easy

  • sudo apt update && sudo apt upgrade && sudo apt autoremove
  • sudo apt install python3.7 python3.7-dev ffmpeg
  • sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
  • sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
  • sudo update-alternatives --config python3 # selecting 3.7
  • python3 -V # should be version 3.7.5 or more
  • DO NOT REBOOT HERE .. YET, gnome terminal will break.

NOOB WORK AROUND: IN LINUX ADVANCED RECOVERY MODE - (SO DO NOT REBOOT YET)

  • $ DO NOT DO sudo update-alternatives --config python3 # select 3.6 It gets your terminal back but is NOT THE SOLUTION.

( Explanation: - [https://pygobject.readthedocs.io/en/latest/getting_started.html#ubuntu-getting-started][1] )

SOLUTION JUST INSTALL WHAT WAS MISSING.

  • python3 -V # must be python 3.7

  • mkdir -p ~/.local/bin ; cd .local/bin

  • vi hello.py # use nano or any editor - it's a test program. See ref link at the end.

        # Test script
        import gi
        gi.require_version("Gtk", "3.0")
        from gi.repository import Gtk
    
    window = Gtk.Window(title=&quot;Hello World&quot;)
    window.show()
    window.connect(&quot;destroy&quot;, Gtk.main_quit)
    Gtk.main()
    

  • sudo update-alternatives --config python3 # set python 3.7

  • python3 hello.py # it does NOT work so install stuff ...

  • sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0

  • sudo apt install libgirepository1.0-dev gcc libcairo2-dev pkg-config python3.7-dev

  • python3 hello.py # it may NOT work still, install MORE stuff

If you pip install to solve, note that it installs for the user only.
For other Linux's or non-pip install please use the link at the end.

  • sudo -k # Notice that pip is NOT SECURITY TRUSTED, turn sudo OFF!
  • python3 -m pip install -U pycairo # this automatically uses pip3
  • python3 -m pip install -U PyGObject # THIS SOLVES THE PROBLEM!
  • python3 hello.py # WORKS! N.B. other uses must install the same!
  • sudo shutdown -r now # OK NOW YOU CAN REBOOT. Gnome Terminal will work.
  • Notice my pip style python3 actually calls pip3 not pip.
  • True cause Ubuntu or Linux Mint: /usr/bin/gnome-terminal calls default python3 which is 3.7 and finds your gi PyGObject include missing so the terminal is unusable with each user missing the control panel.

Conclusion, you get to keep your python upgrade.

References: [1]: PyGObject including gi test https://pygobject.readthedocs.io/en/latest/getting_started.html#ubuntu-getting-started

gowthz
  • 103
2

I fixed the gnome-terminal problem from the solution above.
Enter these commands:

cd /usr/lib/python3/dist-packages/gi/
sudo cp _gi.cpython-36m-x86_64-linux-gnu.so _gi.cpython-37m-x86_64-linux-gnu.so
sudo cp _gi_cairo.cpython-35m-x86_64-linux-gnu.so _gi_cairo.cpython-36m-x86_64-linux-gnu.so
zx485
  • 2,426
2

If you are using Ubuntu22.04, very simple solution here. /usr/bin/gnome-terminal is a python script. You can change 1st line #!/usb/bin/python to #!/usr/bin/python3.8

 #!/usr/bin/python3.8

import string import subprocess import sys import random

from argparse import ArgumentParser, SUPPRESS from gi.repository import GLib, Gio ...

Sons M
  • 21