1

I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):

which python <user>/bin/python/Python-3.6.1/python

I have an executable (chmod 777) sample.py file with this line at the top of the file:

#!<user>/bin/python/Python-3.6.1/python

I can execute the file like this:

python sample.py

But when I run it by itself I get an error:

/full/path/sample.py /full/path/sample.py: Command not found

I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?

EDIT: I tried putting this on top of the file:

#!/usr/bin/env python

Now, I get this error:

: No such file or directory

I tried this to make sure my env is correct

which env /usr/bin/env

EDIT2: Yes, I can run the script fine using the shebang command like this: <user>/bin/python/Python-3.6.1/python /full/path/sample.py

rrlamichhane
  • 1,435
  • 2
  • 18
  • 34
  • Not a solution to the problem, but `chmod 777` is (almost) never the right thing to do. Set only the privileges you actually need. In this case that's most likely `755` (you get read/write/execute, others get read/execute) – viraptor Jun 14 '17 at 23:16
  • Have you checked that you can run the script using the path you're providing? Instead of `python sample.py`, can you run `/the/full/path/from/shbang sample.py`? If yes, then the problem is likely with the formatting of the first line. If no, then either you're pointing at the wrong python binary, or the python installation is broken. – viraptor Jun 14 '17 at 23:21
  • @viraptor, yes I can run the script using the path from shebang, I also added this to my edit above. The formatting is correct, I double checked, and the python installation cannot be broken because I can run scripts using both 'python' command and '/path/from/shebang/' command. – rrlamichhane Jun 14 '17 at 23:27
  • Any spaces or characters that would need quoting in paths? – Borko Jandras Jun 14 '17 at 23:34
  • @BorkoJandras, the path is basically this: `/abc/xyz/disks/ab.xyz.123/myname/bin/python/Python-3.6.1/python`. The only odd thing I see is periods and hyphens. The full path of the python file is also very similar to this. – rrlamichhane Jun 14 '17 at 23:38

2 Answers2

2

Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).

phd
  • 82,685
  • 13
  • 120
  • 165
0

Try using #!/usr/bin/env python as described in this post. Let the OS do the work.

Alex Barry
  • 415
  • 1
  • 9
  • 21