0

I'm working on a little side project and it's my first time using Flask. I'm working with a virtual environment and therefore I have a shebang at the beginning of my script, that looks like this

#!flask/bin/python

When I try to execute my script from the command line, I get the following error:

-bash: ./run.py: flask/bin/python^M: bad interpreter: No such file or directory

I know that this means that the python interpreter couldn't be found, but if I navigate into flask/bin i can see the interpreter inside. Those are all the files inside of flask/bin

activate
activate.csh
activate.fish
activate_this.py
easy_install
easy_install-2.7
pip
pip2
pip2.7
python
python-config
python2 -> python
python2.7 -> python
wheel

I tried to delete my virtual environment directory and create it again, but this didn't help.

Why does my shebang not work in this case?

jss367
  • 4,759
  • 14
  • 54
  • 76
nieka
  • 259
  • 1
  • 4
  • 14

3 Answers3

0
-bash: ./run.py: flask/bin/python^M: bad interpreter: No such file or directory

Bash ignores the shebang line (it's a comment to bash). The shebang line is instead handled by the Unix program loader. If the first two characters of an executable file are #!, the program loader

  • Scans ahead for a new line character, up to a system-specific limit. With Linux, this limit is 127 characters.
  • Truncates trailing whitespace from the end of the line. The Linux kernel code to do this is lines 46 to 56 of binfmt_script.c. A carriage return is not whitespace in this context.
  • Looks for the name of the interpreter, starting with the first non-whitespace character after the leading #!.
  • Tries to execute the named interpreter with the script name as an argument.

In short, the program loader is looking for a file named python^M rather than python in flask/bin. That file does not exist.

The immediate solution is to run dos2unix against your script. The longer term solution is not to use Windows tools to create files targeted for a Unix machine.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
0

Did you write the script in a Windows environment and then move it over to a UNIX environment? Because the line endings are different, files written in one environment will not automatically work on the other. See here for more info.

To resolve this, try using dos2unix, or if you're using something like Notepad++ on Windows, there are often options to save the file in a Windows format.

Mark
  • 56
  • 2
  • Thank you! That helped. I found the command line solution with perl [here](https://stackoverflow.com/questions/6373888/converting-newline-formatting-from-mac-to-windows), since the sed solution didn't work. – nieka Sep 12 '17 at 12:23
-1

flask/bin/python^M, is it because your file was written in windows, and executed in unix?

Jimmy Guo
  • 1,288
  • 1
  • 9
  • 24