I just tried to install python 3.6 on my Ubuntu 16.04 system, and now I cant 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.

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
  • 4
    To add to this: this is caused by a bug in the python3-apt package; there's a Launchpad bug open about it here. – Josh Feb 6 '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

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
  • What does this do exactly? Is it safe? – wjandrea Aug 1 '18 at 19:52
  • 2
    This should be the answer. It is safe @wjandrea , I use it without any problem with Python 3.7 under Ubuntu 18 – Huan Sep 7 '18 at 6:27
  • Can you paste these into xterm? – Tahlor Sep 12 '18 at 21:24
  • Works for me, this should be the answer and I second @Huan – user1282043 Mar 22 at 19:42
  • An explanation of what this does would still be helpful – Daniel Holmes May 24 at 6:21

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 over from python3.5 over python3.6 could resolve the issue, but would break your library consistency. And should be avoided.

  • This fixed everything: Switching back to 3.5. Anyone but me find it excessively stupid that Ubuntu GNOME relies critically on python packages that have the python version number baked into the name? And it doesn't have it's own canned version of python squirreled away so it can't get wedged by an ordinary user action (upgrading Python)? – davidbak 17 hours ago

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.