0

Here is my environment

> lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: Scientific
Description:    Scientific Linux release 6.8 (Carbon)
Release:        6.8
Codename:       Carbon

> /home/ktew/python/bin/python --version
Python 2.7.18

I have added the following line as the first line in my python script.

#!/home/ktew/python/bin/python

The python script is executable.

-rwxrwxrwx 1 ktew ktew   12753 Sep 23 10:35 compare_trims_by_version_5.py*

This line of command is working well.

/home/ktew/python/bin/python compare_trims_by_version_5.py

However, Command not found when I run it.

> ./compare_trims_by_version_5.py
./compare_trims_by_version_5.py: Command not found.
Sam
  • 1,252
  • 5
  • 20
  • 43
  • Could it be an imported module that is not resolving? Try emptying out the script (except your shebang) and put just a print statement and see if it works – John Sep 23 '20 at 02:58
  • I have tried it out but the error remains the same. I even changed the shebang to #!/usr/bin/python (default python) – Sam Sep 23 '20 at 03:22

2 Answers2

0

I have found the answer in https://stackoverflow.com/a/30127747/5057185. I have written the script with windows editor and it contains windows line endings(CR/LF). I used the dos2unix command to fix line endings.

To summarize:

  1. Add the shebang at the first line of script. #!/home/ktew/python/bin/python
  2. Make sure the script is executable. chmod +x script
  3. Use ./script.py instead of script.py in case your directory is not in your PATH
  4. Ensure the line ending is correct (LF instead of CR/LF). Use dos2unix to fix it.
Sam
  • 1,252
  • 5
  • 20
  • 43
-1

The error message looks like the system is trying to run as a shell script instead of using the Python interpreter (ref: Python error: command not found). In this case, nothing seems to be out of place with your Python interpreter. The referred stackoverflow page suggests using #!/usr/bin/env python, recommend you try that as well.

tnwei
  • 860
  • 7
  • 15
  • I have tried it out by changing the shebang to #!/usr/bin/python (default python) but the error remains the same. – Sam Sep 23 '20 at 03:25