0

python blabla.py will execute. But ./blabla.py gives me an error of "no such file or directory" on CentOS6.3.

/usr/bin/env python does open up python properly.

I am new to linux and really would like to get this working. Could someone help?

Thanks in advance!


Note: thanks to all the fast replies! I did have the #!/usr/bin/env python line at the beginning.

which python gives /usr/bin/python as an output.

And the chmod +x was done as well.

The exact error was "no such file or directory" for ./blabla.py, but python blabla.py runs fine.

user1234310
  • 321
  • 1
  • 3
  • 9
  • what's the output of `which python`? – Ariel Sep 30 '12 at 18:23
  • you can execute your script using python `python blabla.py` – dm03514 Sep 30 '12 at 18:25
  • 2
    What is the _exact_ error message? – Roland Smith Sep 30 '12 at 18:32
  • 1
    Problem solved. It turns out to be some Windows/Linux conversion problem. I had initially copied and pasted this file in Windows and moved it to CentOS. After copying and pasting a new blabla.py file in CentOS, it runs properly as an executable. Thanks to all of the comments and help! (Hope this post will be useful to others as well.) – user1234310 Sep 30 '12 at 19:18

3 Answers3

0

Add #!/usr/bin/env python at the head of your script file.

It tell your system to search for the python interpreter and execute your script with it.

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • 4
    It's considered good practice to use `/usr/bin/env python` since that one really does search (yours just executes python using an absolute path, no search is done). – Chris Eberle Sep 30 '12 at 18:26
0
chmod +x blabla.py

should do the trick.

sputnikus
  • 583
  • 1
  • 3
  • 12
0

When you copied the file it contained windows style line endings (called CR LF or "\r\n"). If you execute a script in bash with windows line endings it will treat the CR character as if it were any other character. Thus your interpreter is not looking for /usr/bin/python but /usr/bin/python\r which leads to the no such file or directory error. When you run python through the interpreter as $ python script_name.py the interpreter is aware of windows style line endings so it will work.

In order to fix the issue you need to remove all \r characters. Commands like dos2unix are useful. See this question for further details.

AnnanFay
  • 9,573
  • 15
  • 63
  • 86