0

It is a tough problem. There are several topics for it. But none of them helps me.

I added #!/usr/bin/env python3 (or python), and run test.py, it reported that zsh: command not found: test.py. I was confused. I had tried many forms of shebang. Can you help me?

In following error reports, you can see that the reports are different when running it under HOME path and under the parent path of test.py

[Scripts] test.py                                                     20:51:04
zsh: command not found: test.py
[Scripts] cd ~                                                        20:51:33
[~] Scripts/test.py                                                   20:51:43
env: python\r: No such file or directory

It's not so long since I got the meaning of the shebang line. I hope that it can make my life easy, never writing python before test.py.

Following is the test code.

#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser(description='test')
parser.add_argument('-o', dest='what', action='store', default='hello', metavar='WHAT')

args = parser.parse_args()
print(args.what)

Following is the configuration.

PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:$PATH"

And in terminal,

[~] which python                                                      20:36:55
python: aliased to python3
[~] which python3                                                     20:36:57
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
ls -l
-rwxrwxrwx@ 1 william  staff   273 10 24 20:51 test.py
William Song
  • 405
  • 5
  • 15

2 Answers2

2

Assuming that the directory of test.py is not in your PATH, you will need to use either a relative or absolute path, and ensure that the script has execution privileges.

$ chmod u+x test.py
$ ./test.py

Should execute properly.


With the error env: python3\r: No such file or directory: the file is using "CRLF" newlines: \r\n, while a single \n is expected. So zsh is splitting on the first \n, leaving the shebang line #!/usr/bin/env python3\r, with python3\r obviously not in your PATH. If you change the line endings with unix2dos test.py, that should fix the issue as per this answer.

Kellen
  • 544
  • 5
  • 15
  • ls -l -rwxrwxrwx@ 1 william staff 273 10 24 20:51 test.py – William Song Oct 24 '18 at 13:03
  • @WilliamSong what is the result from using `./test.py` instead of `test.py` without the relative path? – Kellen Oct 24 '18 at 13:12
  • env: python3\r: No such file or directory – William Song Oct 24 '18 at 13:14
  • @WilliamSong it looks like a newlines issue: you can see the kind of line endings with `file test.py` on *nix type systems (including macOS). Make sure the file uses Unix newlines instead of CRLF newlines – Kellen Oct 24 '18 at 13:36
  • 1
    @WilliamSong I can't tell you why the `\r\n` is there, but that's the probable cause of your current problem. If you fix your line endings it should work now, from what I can tell. – Kellen Oct 24 '18 at 14:53
  • I use `dos2unix` to translate it. But it's too much trouble when translating the codes in many files. Can I set it in sublime or vim? I use Mac. – William Song Oct 28 '18 at 01:44
  • @WilliamSong that sounds like a great thing to ask in a search engine. This answer might help you get on the right track with Sublime Text: https://stackoverflow.com/a/11900286/3882704 – Kellen Oct 28 '18 at 15:02
0

After adding the shebang to your python file:

  • Make the file executable chmod +x test.py
  • Run the file while in the directory it exists ./test.py
ikuamike
  • 311
  • 1
  • 6