How transform a python program .py in an executable program in Ubuntu?

Question:

I have a simple python program and I want an executable version (for Ubuntu Linux) of this program to avoid running it in the terminal with python myprogram.py.

How can I do that ?

Asked By: xRobot

||

Answers:

You can try using a module like cxfreeze

Answered By: ghostdog74

At the top op your python program add:

#!/usr/bin/python
Answered By: leonm

There is no need to. You can mark the file as executable using

chmod +x filename

Make sure it has a shebang line in the first line:

#!/usr/bin/env python

And your linux should be able to understand that this file must be interpreted with python. It can then be ‘executed’ as

./myprogram.py
Answered By: relet

As various others have already pointed out you can add the shebang to the top of your file

#!/usr/bin/python or #!/usr/bin/env python

and add execution permissions chmod +x program.py

allowing you to run your module with ./program.py

Another option is to install it the pythonic way with setuptools. Create yourself a setup.py and put this in it:

from setuptools import setup

setup(
    name = 'Program',
    version = '0.1',
    description = 'An example of an installable program',
    author = 'ghickman',
    url = '',
    license = 'MIT',
    packages = ['program'],
    entry_points = {'console_scripts': ['prog = program.program',],},
)

This assumes you’ve got a package called program and within that, a file called program.py with a method called main(). To install this way run setup.py like this

python setup.py install

This will install it to your platforms site-packages directory and create a console script called prog. You can then run prog from your terminal.

A good resource for more information on setup.py is this site: http://mxm-mad-science.blogspot.com/2008/02/python-eggs-simple-introduction.html

Answered By: ghickman

I know the easiest, exact and the best solution. I had the same problem like you but now, I can run my Python/Tkinter(GUI) program with its icon.

As we create .bat files on Windows, we can also create equivalent the .bat files easily in Linux too. Thanks to this file, that, we can start our programs without terminal even if it needs to get command on terminal to start (like Python programs) with double click to its icon (really .png icon 🙂 ) or we can write commands to facilitate our works.
So, how is this going to happen ?

For example, if we want to run our .py program, we just need to write this command to terminal :

python3 locationOfPyFile

So if we create a file that can automatically run this command, problem would be solved. In addition to that, you can have your own icon and even you don’t have to open terminal !

Check this article : Run Commands From It’s Icon (Easiest Way)

Answered By: furkantokac
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.