Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely replace python 3 with python 2 in arch linux

I want to completely replace python 3 with python 2 in arch linux. I have already read https://wiki.archlinux.org/index.php/Python but it only provides a temporary fix. I need to ensure that when I call

#!/usr/bin/python

My program is using python 2 instead of python 3.

like image 788
user1876508 Avatar asked Mar 14 '13 04:03

user1876508


People also ask

How do I switch between versions in Python?

Yes, you should be able to switch between python versions. As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed.

How do I use Python instead of python3 in Linux?

You can try the command line tool update-alternatives . Change the path /usr/bin/python3 to your desired python version accordingly. python2 and python3 are not alternatives.

Are Python 2 and Python 3 are interchangeable?

While Python 2.7 and Python 3 share many similar capabilities, they should not be thought of as entirely interchangeable. Though you can write good code and useful programs in either version, it is worth understanding that there will be some considerable differences in code syntax and handling.


2 Answers

In Arch, /usr/bin/python is actually a symlink to python3. Assuming you've already installed python2, as root, change the symlink to point to python2:

cd /usr/bin
ls -l python
    lrwxrwxrwx 1 root root 7  5 sept. 07:04 python -> python3
ln -sf python2 python
ls -l python
    lrwxrwxrwx 1 root root 7 Dec 11 19:28 python -> python2

If you're using the python2-virtualenv package, then do the same for /usr/bin/virtualenv:

cd /usr/bin
ln -sf virtualenv2 virtualenv
like image 85
Christopher Neylan Avatar answered Sep 28 '22 02:09

Christopher Neylan


Changing the default symlink is a bad idea, and it gets recreated on python3 updates. Instead, create a local python override:

sudoedit /usr/local/bin/python

Paste this inside and save the file:

#!/bin/bash
exec python2 "$@"

Don't forget to make it executable:

sudo chmod +x /usr/local/bin/python

like image 39
Ivan Semkin Avatar answered Sep 28 '22 03:09

Ivan Semkin