Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make python script self-executable [duplicate]

Tags:

python

Possible Duplicate:
Calling a python script from command line without typing “python” first

I've tried

bash$ chmod +x script.py  

doesn't work. I also remember to put

#!usr/bin/env python  

at the beginning of the script.

bash$ ./script.py  

Does nothing, it just changes my cursor to a cross lol

UPDATE: I've fixed

#!/usr/bin/python    

i've also tried

chmod a+x script.py   

still nothing. My script has import commands and uses sys.argv...I've followed the instruction on this link (look at the end of the page). Nothing works

like image 925
BPm Avatar asked Sep 26 '11 23:09

BPm


People also ask

Can Python create standalone executable?

Underneath the GUI is PyInstaller, a terminal based application to create Python executables for Windows, Mac and Linux. Veteran Pythonistas will be familiar with how PyInstaller works, but with auto-py-to-exe any user can easily create a single Python executable for their system.

Can Python script be run as an executable?

On Windows, the standard Python installer already associates the . py extension with a file type (Python. File) and gives that file type an open command that runs the interpreter ( D:\Program Files\Python\python.exe "%1" %* ). This is enough to make scripts executable from the command prompt as 'foo.py'.

What are the 2 ways to execute Python program?

It can run a Python code in two ways: As a Script or Module. As a piece of code written in an interactive session.


2 Answers

the "shebang" needs to contain the full path to the executable. You're calling env, which is good, but you haven't given it the full path: start your script like so:

#!/usr/bin/env python  
# ^
like image 73
SingleNegationElimination Avatar answered Sep 21 '22 12:09

SingleNegationElimination


Here is the list of things to try, in rough order of likelihood:

  • Ensure that the shebang line has correct syntax (you've done this already, #!/usr/bin/python).
  • Make sure the shebang is the first line in the file (not even a blank line or a comment above it).
  • Verify that /usr/bin/python actually exists and works. Your Python interpreter may be installed elsewhere. Type /usr/bin/python at a prompt and make sure Python starts. Type which python if you don't know where it is installed.
  • If . is not in your PATH (it may not be), you must run your script with ./script.py because the shell does not look for commands in the current directory by default.
  • Make sure the executable bit is set on your script (+x, verify with ls -l).
  • Make sure that you are using LF only line endings in your editor. Shells can be picky, and your shebang line must end with LF only and not CRLF. This is only likely to be a problem if you're using a Windows text editor, but it might be worth checking.
  • Make sure that your text editor does not silently insert a UTF-8 BOM at the start of the file. Again, this is only likely if you're using Notepad on Windows.
like image 34
Greg Hewgill Avatar answered Sep 20 '22 12:09

Greg Hewgill