I have a simple python program .py and I want an executable version ( for UBUNTU LINUX ) of this program and avoid to run it by terminal with "python myprogram.py". How can I do that ?
Add the following at the top of your program:
#!/usr/bin/env python
The above line specifies that your program should be executed by invoking the Python interpreter with a clean environment (that's what /usr/bin/env
does).
After that, make the program executable from the command line for all users:
$ chmod a+x your_program.py
Note that this does not make your program to be able to run without the Python interpreter, i.e. you cannot copy it to another computer where the Python interpreter is not installed.
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
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