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.
3 Answers
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
1 Comment
#! /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..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:
- Use either the Nautilus file properties dialog or the chmod command to mark it executable (
chmod +x whatever.pyw) - Make sure that the first line in the file says
#!/usr/bin/env python(See wikipedia for more info) - 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
You have to set the permission for the file for it to be executable using chmod. See the manpages for chmod for details.