Ubuntu Python shebang line not working
Asked Answered
D

6

9

Unable to get shebang line working in Ubuntu for python script. I only get a command not found error each time.

test.py

#!/usr/bin/env python

print ('!')

Ran

:which python
/usr/bin/python

Played around with different locations for python in the shebang but no luck including what was provided by which python. Any tips on how to troubleshoot this?

Thanks

Diseuse answered 11/2, 2013 at 21:17 Comment(7)
Make sure your line endings are unix \n newlines.Dismast
I made the test file in nano. I usually use sublime but this is on my server so just whipped it up in nano. I honestly have no idea how to view special chars in nano. I just assumed since I made it on linux it would handle the new line appropriately.Diseuse
It probably is unix newlines then; is the file executable? (chmod +x scriptname.py)Dismast
it is indeed. Not sure if this helps but if I run (/usr/bin/python test.py) it runs like a champDiseuse
What does file test.py say?Dismast
If you're using UTF-8, copied from Windows, remember to remove BOM as wellObelia
Try running dos2unix <myscript> to fix the line endings and then try you script again.Pyrites
O
19

If you are trying to run the command as

$ test.py

the error may not have anything to do with the shebang. Rather, the directory that test.py resides in is not in your PATH. Try

$ ./test.py

to bypass PATH lookup.

(This is in addition to making sure that the script itself is executable.)

Ottinger answered 11/2, 2013 at 21:36 Comment(5)
ugh that was my primary problem in testing, can you explain how the logic of ./test.py works?Diseuse
When your command consists of a single word, the shell looks for that command in each directory stored in PATH, starting with the first, and using the first match it finds. The current directory (.) is typically not in PATH, as it could be a security risk, so you need to specify the exact path as ./test.py (the file test.py in the current directory).Ottinger
Not to keep asking but why is PATH a security risk?Diseuse
@DrewK: adding the current directory to PATH is considered a security risk because an attacker would be able to "override" standard commands by placing a script in the current directory. Imagine a custom ls script which acts exactly as normal ls except not showing directories called .evil-hidden-stuff...Unexacting
I've never seen it stated this way, but I think the recommendation against putting '.' in your PATH would extend to any relative path. The idea is that the lookups should conform to a fixed set of directories that does not depend on your current working directory.Ottinger
A
9

On the python docs page it says:

To easily use Python scripts on Unix, you need to make them executable, e.g. with

$ chmod +x script and put an appropriate Shebang line at the top of the script. A good choice is usually

#!/usr/bin/env python which searches for the Python interpreter in the whole PATH. However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python as the interpreter path.

I don't know if this applies for you or not.

Argillaceous answered 11/2, 2013 at 21:22 Comment(1)
I thought that my apply and I had tried use those paths and could not get it to run.Diseuse
P
6

Apart from executing the script with a preceding dot or making it executable, there might be another issue:

If you try to use a script written with a windows editor, it may contain windows line endings. Removing these can make the shebang work again.

To remove such line endings, refer to How to convert Windows end of line in Unix end of line (CR/LF to LF) for instance.

See also my general remarks on failed shebang evaluations at my other answer.

Pollaiuolo answered 8/5, 2015 at 15:40 Comment(0)
D
2

Make sure that the "FIRST LINE" is the shebang. Do not give any newline character in the beginning of the file. "No newline character in beginning"

Demonology answered 3/10, 2015 at 14:27 Comment(0)
P
0

This may be due to a kernel misconfiguration. Take a look at your kernel's config options, and check if CONFIG_BINFMT_SCRIPT is set:

zcat /proc/config.gz | grep CONFIG_BINFMT_SCRIPT

If the output of this command is anything besides CONFIG_BINFMT_SCRIPT=y, this means that your kernel will not allow you to use shebangs. You will need to get a new kernel or recompile your current kernel with CONFIG_BINFMT_SCRIPT=y.

Petr answered 11/3, 2019 at 20:52 Comment(0)
S
0

You can also check your line ending in gitbash/pycharm terminal by typing,

cat .gitattributes

this will list the setup, for linux setup paste this *.sh text eol=lf

if this doesnt work, then better create a new branch and delete the exisiting file and create newfile.

This worked for me

Sora answered 12/5, 2022 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.