2

I've created a simple python script and therefor have a .py file. I can run it from the terminal but if I double click it only opens up in gedit. I've read this question other places and tried the solutions, however none have worked. I'm running Ubuntu 13.04, I've selected the box to make the file executable. I've even installed a fresh instance of Ubuntu 13.10 on another computer and it does the same thing. What might I be missing here?

4

5 Answers 5

7

I had pretty much the same problem. I finally came across the solution at Ask Ubuntu. You have to change the nautilus file manager setting. Go to

Edit -> Preferences -> Behavior

and select the option as shown in the picture. Select Ask each time if you want to avoid unwanted execution of the script.
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

6

You have to give the file execution permission:

~ $ sudo chmod u+x your_file.py

You should also add this as the first line of the file to tell the system about the program it should use to execute your script:

#!/usr/bin/env python

This will search your environment (env) for the path of python. If you want it to execute in Python 3, replace python with python3.

Then your system should automatically ask you whether you want to run the program or to show the code.

2 Comments

Thanks for the help, but I've just now tried both and still same result. I'm using python 2.7 FYI. And just to be sure, I can see that the permissions are "Owner=Read and write", "Group=Read and write" "Others=Read-only"
@Alfred Are you sure that "Allow executing file as program" is checked? On my system (Linux Mint, but it should be very similar to Ubuntu), the command I posted is enough to enable execution, and when I click it a message pops up saying: Do you want to run "script.py", or display its contents? "script.py" is an executable text file. [Run in Terminal] [Display] [Cancel] [Run]".
1

adding to @pascalhein,

if you have anaconda/miniconda installed your hashbang should be something like:

#!/home/geoff/miniconda3/bin/python

which you can find with the command "which python" in your terminal

Comments

0

Add to the top of the file:

#! /usr/bin/env python

Comments

0

In my case it works after includes at the first line: #!/home/yourusername/anaconda3/bin/python You can check the appropiate path running which python in your console.

It is also neccessary to change the file manager setting and configure it to run your scripts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.