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.
2
  • 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 Commented Sep 23, 2020 at 2:58
  • I have tried it out but the error remains the same. I even changed the shebang to #!/usr/bin/python (default python) Commented Sep 23, 2020 at 3:22

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I have tried it out by changing the shebang to #!/usr/bin/python (default python) but the error remains the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.