Take the 2-minute tour ×

After very few issues only I moved my application from Python2 to Python3 making sure it will still run with Python 2.7, and hence has python >= 2.7 as dependency only.

This was mainly done because Python3, and some dependencies are not installed in a default 12.04 LTS, and I do not want my users to have to install all Python3 only to run my script.

When I create an appname.desktop launcher I now need to decide if it starts my application using Python2, or Python3 like

EXEC=python /path/app.py
EXEC=python3 /path/app.py

But what I would like it to do is to

  1. Launch the application with the Python3 interpreter if Python3 is installed.
  2. Otherwise use Python2 if Python3 is not installed.

How can this be done? Do I need to tell it in my package installation script, or can I have a launcher which can handle both (in case people install Python3 after they had installed my script)?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

It sounds too obvious to a good answer but...

Why you don't use a bash script as your app binary. I mean drop in /usr/bin/ a bash script that checks for python3 and then runs the real script.

The launcher will just point to the bash script.

It will even work from the command line (as only the bash script will be on PATH).

share|improve this answer
Thank you. That's how I do it right now. Thing that confused me in the first place was what happened in case people install Python3 after my script (which then could also switch to Pyhon3). This obvioulsy can best be done from a script - sometimes the obvious is the best ;) –  Takkat Dec 3 '12 at 8:10
add comment

I would do this in my setup.py script. Roughly like this (untested):

import sys

template = """[Desktop Entry]
Exec=python%s /path/app.py

version = '' if sys.version_info.major == 2 else '3'
with open('myapp.desktop', 'w') as dfile:
    dfile.write(template % version)
share|improve this answer
isn't it, that at present the setup.py would always be executed from Python2, even when Python3 is installed? It will then never give me a version 3 from sys.version. –  Takkat Nov 30 '12 at 11:11
If the user wants your module to be installed for Python 3, it needs to be installed through python3 setup.py install. Otherwise, your files will be placed in the Python 2 packages folder and won't be accesible by Python 3. –  Timo Nov 30 '12 at 12:06
It will be installed from a DEB. Manual installs never are an issue ;) –  Takkat Nov 30 '12 at 12:28
Hmm, I guess you could use the prerm script for that: wiki.ubuntu.com/PackagingGuide/Basic#postinst_and_prerm . It's just a bash script, so you can check if the python3 executable exists and base your desktop file upon that. –  Timo Nov 30 '12 at 14:41
I'm sorry. My previous comment should have been about postinst ofcourse, not prerm. I can't edit it anymore. –  Timo Nov 30 '12 at 16:47
show 1 more comment

Your Answer


