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

5
  • 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) Commented Jun 14, 2017 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. Commented Jun 14, 2017 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. Commented Jun 14, 2017 at 23:27
  • Any spaces or characters that would need quoting in paths? Commented Jun 14, 2017 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. Commented Jun 14, 2017 at 23:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You're a lifesaver. That's it. Goddammit! I ran dos2unix sample.py and now it runs fine. Thanks a lot!
0

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

1 Comment

Thanks for the response. I tried that but it doesn't work. Please look at my edit for more detail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.