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.

1
  • This has nothing to do with .pyw files (they just prevent the console from appearing but are otherwise exactly the same). Commented Aug 30, 2010 at 19:07

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

.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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.