Alex Rivera | Logout

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

Asked 2010-10-18T09:04:37.327
30

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 ?

Edit
Report

2 Answers

31

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 2010-10-18T12:04:41.483
2

At the top op your python program add:

#!/usr/bin/python
answered 2010-10-18T09:07:52.847

Your Answer