2

I think I have break my system by changing the default python.

So when I run : python --version, I get: Python 3.5.3

When I run ls /usr/bin/python*, I get:

/usr/bin/python   /usr/bin/python2.7         /usr/bin/python2-config  /usr/bin/python3.5         /usr/bin/python3.5m         /usr/bin/python3-config  /usr/bin/python3m-config
/usr/bin/python2  /usr/bin/python2.7-config  /usr/bin/python3         /usr/bin/python3.5-config  /usr/bin/python3.5m-config  /usr/bin/python3m        /usr/bin/python-config

By changing the default python, I am getting a lot of errors: For example, when I run the command sudo apt-get autoremove, the following appears:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up python-pep8 (1.7.0-4) ...
Traceback (most recent call last):
  File "/usr/bin/pycompile", line 38, in <module>
    from debpython.namespace import add_namespace_files
  File "/usr/share/python/debpython/namespace.py", line 120
    except (IOError, OSError), e:
                             ^
SyntaxError: invalid syntax
dpkg: error processing package python-pep8 (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 python-pep8
E: Sub-process /usr/bin/dpkg returned an error code (1)

I did try to go back by looking at the How to update-alternatives to Python 3 without breaking apt?

I tried: sudo update-alternatives --config python

But the results were:

There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3
Nothing to configure.

How to go back to my default python?

EDIT: Output of ls -l /usr/bin/python*

lrwxrwxrwx 1 root root      24 Mar  9 21:35 /usr/bin/python -> /etc/alternatives/python
lrwxrwxrwx 1 root root       9 Sep 23  2019 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3772872 Nov 23  2017 /usr/bin/python2.7
lrwxrwxrwx 1 root root      33 Nov 23  2017 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root      16 Jan 24  2017 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root       9 Sep 23  2019 /usr/bin/python3 -> python3.5
-rwxr-xr-x 2 root root 4747720 Nov 23  2017 /usr/bin/python3.5
lrwxrwxrwx 1 root root      33 Nov 23  2017 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config
-rwxr-xr-x 2 root root 4747720 Nov 23  2017 /usr/bin/python3.5m
lrwxrwxrwx 1 root root      34 Nov 23  2017 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root root      16 Jan 20  2017 /usr/bin/python3-config -> python3.5-config
lrwxrwxrwx 1 root root      10 Sep 23  2019 /usr/bin/python3m -> python3.5m
lrwxrwxrwx 1 root root      17 Jan 20  2017 /usr/bin/python3m-config -> python3.5m-config
lrwxrwxrwx 1 root root      16 Jan 24  2017 /usr/bin/python-config -> python2.7-config
Ridwan
  • 214
  • 4
  • 17
  • 2
    How did you change your default Python in the first place? – AKX Mar 29 '21 at 19:35
  • Can you clarify what you mean by "changing the default python"? Do you change the Python 2 symlink to point to a Python 3 interpreter, or did you remove your system's required Python 2 interpreter altogether? – Brian61354270 Mar 29 '21 at 19:36
  • @AKX, i think I did `update-alternatives --install /usr/bin/python python /usr/bin/python3 1`, the problem is that i don't really remember! – Ridwan Mar 29 '21 at 19:39
  • @Brian, I think I changed the Python 2 symlink to point to a Python 3 interpreter. – Ridwan Mar 29 '21 at 19:40
  • It kind of sounds like the `/usr/bin/python` is symlinked to `/usr/bin/python3`. Can you edit your question with the output of `ls -l /usr/bin/python*`? – user8408080 Mar 29 '21 at 19:40
  • Does this answer your question? [How to update-alternatives to Python 3 without breaking apt?](https://stackoverflow.com/questions/43062608/how-to-update-alternatives-to-python-3-without-breaking-apt) – Brian61354270 Mar 29 '21 at 19:40
  • @user8408080, i edited it. – Ridwan Mar 29 '21 at 19:52

1 Answers1

0

I'm guessing you used to work with python2.7 and now you are using 3.5 by default.

One way of getting back the old & good python2.7 is to edit your ~/.bashrc with adding an alias of alias python='/usr/bin/python2.7'.

Don't forget to source it (source ~/.bashrc) :)

Hook
  • 355
  • 1
  • 7
  • After sourcing the file? Did you try to run python --version? What do you see? – Hook Mar 29 '21 at 20:00
  • I do get `Python 2.7.13` now, but the invalid syntax is still here for the example mentioned in the question. – Ridwan Mar 29 '21 at 20:03
  • That is because you are running ```sudo apt-get autoremove``` which uses python alias (if exist) from ```/root/.bashrc```. The question is first of all why do you have to use ```sudo``` for those command? Did you try running it without root permissions (i.e - ```apt-get autoremove```)? Another suggestion is to work under virtual environment using ```conda``` and by that you can control your python version in each ```venv```. – Hook Mar 29 '21 at 20:12