Search code examples
pythonshellexecutabletcsh

Python script with executable permission says "Command Not Found"


I have a python script abc.py. Inside a shell script file, I call it ./abc.py

Inside abc.py, at the top, I have #!/usr/bin/python

test -x abc.py && echo true || echo false return true

On executing the shell script, it says ./abc.py: Command not found.

It works if I call it as python ./abc.py instead of just ./abc.py

The issue is only with this abc.py file that I created today. The older python scripts in the same directory with different purpose and names work without any issue.

I have referred the correct path of the file BTW. What could be the issue?


Solution

  • In tcsh, this happens when the interpreter is invalid:

    $ cat foo
    #!/invalid
    
    $ tcsh -c './foo'
    ./foo: Command not found.
    
    $ bash -c './foo'
    bash: ./foo: /invalid: bad interpreter: No such file or directory
    

    This could be for several reasons. Perhaps your path is wrong for your system:

    % type python
    python is /usr/local/bin/python
    

    in this case the script needs to start with #!/usr/local/bin/python instead.

    Alternatively, the script could have Windows line endings (carriage returns):

    $ cat -v foo
    #!/usr/bin/python^M
    ...^M
    

    In this case, save it with Unix line endings instead.