11

I updated my system (Ubuntu 18.04) from Python 3.6 to Python 3.8, and reset the defaults so that python3 now points to Python 3.8 (and not 3.6). However, since then, the terminal has refused to open using Ctrl + Alt + T, and other obvious methods such as clicking on the icon itself.

When I run gnome-terminal - I get the following:

usernew@HP:/usr/lib/python3/dist-packages/gi$ gnome-terminal
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' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)

I don't know what this means but I guess it definitely points to the fact that something went wrong during the update. I understand that there are other existing threads on similar issues, but most of them were about updating from Python2 to Python3, so I'm not sure if they're relevant.

Could someone help, please?

Important Update: So, after reading this answer - I changed the gnome-terminal script's first line to #!/usr/bin/python3.6 instead of #!/usr/bin/python3.8 - and that solves the problem.

Also, when I type python3 in the terminal, I'm greeted with Python 3.8.2, as desired.

The question remains - Why did this work? What was the actual problem? An explanation would help, so I really know what I'm doing.

Thanks!

vvvvv
  • 25,404
  • 19
  • 49
  • 81
stoic-santiago
  • 239
  • 3
  • 8

1 Answers1

13

You shouldn't change the symlink /usr/bin/python3 since a bunch of Ubuntu components depend on it, and Ubuntu-specific Python libraries like gi are built only for the Python build shipped with Ubuntu, which is version 3.6 on 18.04.

See Gnome terminal will not start on Ask Ubuntu (though note that it's about Ubuntu 16.04 which uses Python 3.5). So the best way to fix it is to revert the symlink:

sudo ln -sf python3.6 /usr/bin/python3

As for setting Python 3.8 as the default, you could put an alias in your bashrc:

alias python3=python3.8

But this will only affect the shell for your user. In scripts for example if you want to use Python 3.8 you'll have to write it, i.e. #!/usr/bin/env python3.8

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Are there ways other than setting an alias for python3 to point to python3.8? – stoic-santiago Apr 10 '20 at 17:13
  • 1
    There are other ways, like putting a symlink in your user PATH `python3 -> /usr/bin/python3.8`, but I think there's still some potential for causing problems or at least confusion. Personally I don't touch the defaults at all; if I want to run a specific version of Python, I write it explicitly. But I'm not a professional developer, so take this with a grain of salt. – wjandrea Apr 10 '20 at 17:17
  • 1
    Also, I just curious - what happens when I use commands like pip3 install? Is python 3.6 used, or 3.8? There's nothing like pip3.8 install, as far as I know. If 3.6 is being used - how can I use 3.8? – stoic-santiago Apr 10 '20 at 17:22
  • @jughead.ai It depends on your setup. You can get `python3-pip` from the Ubuntu repos which includes `pip3` and uses 3.6, but when you installed Python 3.8 it might have set `pip3` to use 3.8. Also note pip in the Ubuntu repos is always sorely out of date. Anyway, the best way I've found to use pip with a different Python version is a virtualenv. – wjandrea Apr 10 '20 at 17:32
  • Thanks for mentioning that a bunch of system components relies on libs of 3.6. Even the gnome-terminal. – WesternGun Aug 06 '21 at 14:06