views:

105

answers:

3

In windows to make one of my codes execute all I have to do is double click on the file. However, I can't seem to figure out how to do a similar task in Ubuntu.

+5  A: 

Make sure you have #!/usr/bin/env python as the first line of your script, then in your shell do:

chmod +x file.py
./file.py
Wooble
To the OP: This is what you need, just don't forget to add something along the lines of `#! /usr/bin/python` as the first line. the `#!` is the unix-y way of telling the computer what executable is needed to run the script.
Joe Kington
Merge the comment into the answer and we're done.
delnan
A: 

You have to set the permission for the file for it to be executable using chmod. See the manpages for chmod for details.

MAK
+3  A: 

.pyw files are just .py files that have been renamed so that Windows file associations will launch them with the console-free Python interpreter instead of the regular one.

To get run-on-doubleclick working on Ubuntu, first, you need to make sure the kernel sees the script as executable and knows what to do with it. To do that:

  1. Use either the Nautilus file properties dialog or the chmod command to mark it executable (chmod +x whatever.pyw)
  2. Make sure that the first line in the file says #!/usr/bin/env python (See wikipedia for more info)
  3. Make sure the file was saved with Unix-style LF (\n) line-endings rather than DOS/Windows-style CRLF (\r\n) line-endings. (The kernel expects Unix-style line endings for step 2 and, if you forget, it sees the CR (\r) character as part of the path and errors out)

You can test whether you've completed these steps properly by running your script in a terminal window. (cd to the directory it's in and run ./your_script.pyw)

If it works, then Nautilus should just automatically display an "Edit or run?" dialog when you double-click. However, it's been a while since I've used GNOME, so I can't be sure.

If it doesn't, try renaming the file to .py. (I remember Nautilus having a "Extension matches header?" safety check which may not be aware that .pyw is a valid synonym for .py)

ssokolow