This question already has an answer here:

My Python files opens in gedit instead of Python. Is there a way to fix this?

I have tried to allow them to execute in the properties setting, but that didn't work. I can open them in the terminal, but not in the "usual" way.

marked as duplicate by Florian Diesch, Jacob Vlijm, Eric Carvalho, Aditya, g_p Dec 19 '14 at 5:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Eh what do you mean "open in python"? You either edit with an editor (like gedit) or you 'execute' it with something like "python file_to_execute". – Rinzwind Dec 17 '14 at 16:49
  • 1
    Do you have #!/usr/bin/python as the first line in the file? What do you mean by the "usual" way? – Ahti Komu Dec 17 '14 at 16:51
  • Yes I want to execute the file. I'm sorry, I'm really new to this. – Gringottzz Dec 17 '14 at 20:57

There's two ways we could interpret this - you don't specify whether you want the script to be run by the Python interpreter, or if you want it to open the script in IDLE.

1. You want to execute the script.

While we can't execute the script directly from within Nautilus, we can create a .desktop file that will launch the script for you. Simply open up gedit, nano, or whatever editor you want and enter the following:

[Desktop Entry]
Exec=python /path/to/your/code.py
Icon=/path/to/icon # not necessary, just for looks
Name=Name of your script
Terminal=true # Tells the system to open it in a terminal

Save this somewhere. You can save anywhere, but if you want it accessible/launchable from the dash you'll need to save it in ~/.local/share/applications/.

2. You want to open the script in IDLE

As far as I can tell, you can't actually open a Python script directly in IDLE. Strange, I know. Granted, I don't use IDLE that much (if at all), so it could just be the way my system is set up.

You can, however, create another .desktop file that will launch IDLE with your script.

[Desktop Entry]
Exec=idle-python3.4 /path/to/your/code.py
Icon=/path/to/icon # not necessary, just for looks
Name=Name of your script

You'll notice that the command is idle-python3.4. This answer is assuming you are running Ubuntu 14.04 (default Python 3 installation is 3.4). However, if you have or use a different version, then you'll need to change this line to be the correct command for your IDLE version.

You'll also notice we dropped the Terminal=true line the second time. This is because IDLE doesn't need to be launched from the terminal.

  • Not sure if I'm a fan of this answer. Looks too complicated. – don.joey Dec 17 '14 at 17:16
  • 1
    @don.joey What do you mean? All I'm doing is showing him how to make a .desktop file for either of the two options I mentioned. – RPiAwesomeness Dec 17 '14 at 17:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.