apache2 using python2.7 & I want to use python3.4

10,097

Solution 1

I think you have installed mod_wsgi for python2 with this command:

sudo apt-get install libapache2-mod-wsgi

But if you want to use mod_wsgi with python3, you should install correct mod_wsgi with this command:

sudo apt-get install libapache2-mod-wsgi-py3

Solution 2

To change a python version on per user basis you simply create an alias within user's home directory. Open ~/.bashrc file and add new alias to change your default python executable:

alias python='/usr/bin/python3.4'

Once you make the above change, re-login or source your .bashrc file:

$ . ~/.bashrc

Check your default python version:

$ python --version
Python 3.4.2

Change python version system-wide

To change python version system-wide we can use update-alternatives command. Logged in as a root user, first list all available python alternatives:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python     (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

The --install option take multiple arguments from which it will be able to create a symbolic link. The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.4 and as a result the /usr/bin/python3.4 was set as default python version automatically by update-alternatives command.

# python --version
Python 3.4.2

Next, we can again list all python alternatives:

# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4

From now on, we can anytime switch between the above listed python alternative versions using below command and entering a selection number:

# update-alternatives --config python
Share:
10,097
saudi_Dev
Author by

saudi_Dev

Updated on June 17, 2022

Comments

  • saudi_Dev
    saudi_Dev about 2 years

    I setup my server using python bottle & mod_wsgi my bottle script are all writing by python3.4 but the apache2 server using by default python2.7.6 ?

    is there a way to make the python3.4 the default python on apache2

    I'm stock right now

  • Graham Dumpleton
    Graham Dumpleton over 7 years
    You would still need to install a version of mod_wsgi compiled against the desired Python version. You can't make it use a different version than was compiled for.
  • Nour
    Nour over 7 years
    @GrahamDumpleton Thanks for the adding & Yes you are right I was talking about having two versions inside the server however I'll modify the answer as you mentioned )
  • blueDexter
    blueDexter almost 3 years
    if module wsgi is not already enabled you can do the same with sudo a2enmod wsgi and also reload apache2 server with sudo systemctl reload apache2.service