66

Cannot figure out, where to change EOF in PyCharm. My scripts start with :

#!/usr/bin/python # -*- coding: utf-8 -*- 

It outputs something like this when I try to run it like an executable (chmode +x) :

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

2
  • 4
    there seems to be ^M characters in the script, use dos2unix Commented Apr 2, 2012 at 10:50
  • It seems like the original file was created in MSDOS/Windows without UNIX/Linux end of line support. So when you tried to run this file on UNIX/Linux, you got an error. The easiest way is to recreate the file on UNIX/Linux. The scalable way is to configure the MSDOS/Windows shell to support saving with UNIX/Linux end of lines. It's help you to create and reuse files on both systems. Commented Jun 29, 2018 at 23:25

10 Answers 10

55

The issue is not EOF but EOL. The shell sees a ^M as well as the end of line and thus tries to find /usr/bin/python^M .

The usual way of getting into this state is to edit the python file with a MSDOS/Windows editor and then run on Unix. The simplest fix is to run dos2unix on the file or edit the file in an editor that explicitly allows saving with Unix end of lines.

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

3 Comments

I think the python interpreter is not ran at all. The shebang interpreter fails on ^M.
Yes sorry the shell is see ingthis
If you don't have dos2unix, simply execute sudo apt-get install dos2unix, in Ubuntu 14.04.
38

Set line separator to Unix:

Unix

1 Comment

If the file has already been saved, this doesn't seem help. Probably only with new files? You'll need to use the dos2unix tool for those files.
33

You may find the answers here: ./configure : /bin/sh^M : bad interpreter

As a Mac OS X user, I didn't find the command dos2unix. Alternatively, I use vi/vim: :set fileformat=unix and then save the file :wq

Comments

25

If you are using Vim, just enter the following command:

:set fileformat=unix 

Comments

16

you may want to try dos2unix <filename>

1 Comment

note. Had centos installed without it. to install just sudo yum install dos2unix. it's in the repos.
11

Install dos2unix: sudo apt-get install dos2unix

and let it do the magic: dos2unix FILENAME

Comments

6

For MacOS you can install it via Homebrew like this:

brew install dos2unix 

And next do

dos2unix FILENAME 

Comments

3

Similar to Jiangwei Yu's post. On UNIX/Linux, I used vi to edit the Python file. Using vi, you can see the ^M at the end of each line.

Find the following line /usr/bin/python^M

Hit end to get to the end of the line

Hit delete to remove the ^M

To save the file and quit, type in: :wq

This worked for me.

Comments

3

you may try to do this:

sed --in-place 's/^M//g' main.py 

[ to type in ^M, press ctrl+v,ctrl+m ]

Comments

2

Just a Question of format beween win and unix:

try command: dos2unix fileName

After it run again, it should work

1 Comment

How does this add to or improve on other answers?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.