开发者

Making python files executable in Ubuntu

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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜