PDA

View Full Version : [ubuntu] python- run a .py file - default action is gedit



marshall31415
August 29th, 2009, 03:39 PM
Hi.

I am used to coding python in windows, and in windows when i double click on any .py file the default action is to execute it through a command like terminal (not IDLE). In Ubuntu however, the default action is to open it with "gedit" text editor. I have created a custom open command to get it to default open with IDLE for editing - but how can I get it to run in a terminal like windows does it when I double click the file.

I want to do this so I can just double click a file that I've made and have it run instantly. I dont want it to run in IDLE as I want it to close upon executing, like windows does.

Thanks

rajeev1204
August 29th, 2009, 04:49 PM
Hi.

I am used to coding python in windows, and in windows when i double click on any .py file the default action is to execute it through a command like terminal (not IDLE). In Ubuntu however, the default action is to open it with "gedit" text editor. I have created a custom open command to get it to default open with IDLE for editing - but how can I get it to run in a terminal like windows does it when I double click the file.

I want to do this so I can just double click a file that I've made and have it run instantly. I dont want it to run in IDLE as I want it to close upon executing, like windows does.

Thanks

You can specify x-term to run it in a terminal by right click the file and open with.

red_spyder
August 29th, 2009, 05:03 PM
If you want to run it through the terminal, you need to install python first. Type:
sudo apt-get install python
insert your password and let it install on its own. If it asks to confirm the download, just type 'y' and hit enter.
When you get full control of the computer again, go to the directory the .py file is located through the terminal and to execute it type 'python *.py' where * is the name of your file.

Copernicus1234
August 29th, 2009, 05:18 PM
I dont know how Windows does it, but if you just want to run it instantly when double clicking on it, you can just right click the file, go to Properties, and under the Permissions tab, check "Allow this file to run as a program".

In your python file, add this at the top:


#!/usr/bin/env python

Now when doubleclicking that file, Linux will know what program to use to run it and it will just run as a ordinary program.

rajeev1204
August 29th, 2009, 09:01 PM
If you want to run it through the terminal, you need to install python first. Type:
sudo apt-get install python
insert your password and let it install on its own. If it asks to confirm the download, just type 'y' and hit enter.
When you get full control of the computer again, go to the directory the .py file is located through the terminal and to execute it type 'python *.py' where * is the name of your file.

python is installed by default on all linux distros.

donkyhotay
August 29th, 2009, 09:03 PM
In your python file, add this at the top:


#!/usr/bin/env python

Now when doubleclicking that file, Linux will know what program to use to run it and it will just run as a ordinary program.

+1 This is the easiest way of doing it IMHO.

marshall31415
August 30th, 2009, 06:53 AM
I dont know how Windows does it, but if you just want to run it instantly when double clicking on it, you can just right click the file, go to Properties, and under the Permissions tab, check "Allow this file to run as a program".

In your python file, add this at the top:


#!/usr/bin/env pythonNow when doubleclicking that file, Linux will know what program to use to run it and it will just run as a ordinary program.

Thanks very much for your help. This worked

Thanks to everyone else who answered too.