Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a shebang causes No such file or directory error when running my python script

Tags:

python

shell

I'm trying to run a python script. It works fine when I run it:

python2.5 myscript.py inpt0 

The problem starts when I add a shebang:

#!/usr/bin/env python2.5 

Result in:

$ myscript.py inpt0 : No such file or directory 

Try 2:

#!/usr/local/bin/python2.5 

Result in:

$ myscript.py inpt0 : bad interpreter: No such file or directoryon2.5 

When I run them directly in the terminal they both work just fine:

$ /usr/local/bin/python2.5 Python 2.5.4 (r254:67916, Feb  9 2009, 12:50:32) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>  $ /usr/bin/env python2.5 Python 2.5.4 (r254:67916, Feb  9 2009, 12:50:32) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 

Any hints on how to make this work with shebang?

like image 680
Pe2 Avatar asked Feb 10 '09 07:02

Pe2


People also ask

Do you need shebang for python script?

You should add a shebang if the script is intended to be executable. You should also install the script with an installing software that modifies the shebang to something correct so it will work on the target platform.

What is the purpose of using a shebang line in a python file?

The shebang line was invented because scripts are not compiled, so they are not executable files, but people still want to "run" them. The shebang line specifies exactly how to run a script.

What is #!/ Usr bin python in python?

By specifying #!/usr/bin/python you specify exactly which interpreter will be used to run the script on a particular system. This is the hardcoded path to the python interpreter for that particular system. The advantage of this line is that you can use a specific python version to run your code.


1 Answers

I had similar problems and it turned out to be problem with line-endings. You use windows/linux/mac line endings?

Edit: forgot the script name, but as OP says, it's dos2unix <filename>

like image 89
kender Avatar answered Sep 18 '22 09:09

kender