PyCharm. /usr/bin/python^M: bad interpreter

PythonInterpreterEofPycharm

Python Problem Overview


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

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

Outputs something like this, when I try to run it like executable (chmode +x):

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

What to do and how to be?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

Set line separator to Unix:

Unix

Solution 3 - Python

You may find the answers here: https://stackoverflow.com/questions/2920416/configure-bin-shm-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

Solution 4 - Python

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

:set fileformat=unix

Solution 5 - Python

you may want to try dos2unix <filename>

Solution 6 - Python

Install dos2unix: sudo apt-get install dos2unix

and let it do the magic: dos2unix FILENAME

Solution 7 - Python

For MacOS you can install it via Homebrew like this:

brew install dos2unix

And next do

dos2unix FILENAME

Solution 8 - Python

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.

Solution 9 - Python

you may try to do this:

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

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

Solution 10 - Python

Just a Question of format beween win and unix:

try command: dos2unix fileName

After it run again, it should work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwoozlyView Question on Stackoverflow
Solution 1 - PythonmmmmmmView Answer on Stackoverflow
Solution 2 - PythonCrazyCoderView Answer on Stackoverflow
Solution 3 - PythonJiangwei YuView Answer on Stackoverflow
Solution 4 - PythonVitaly IsaevView Answer on Stackoverflow
Solution 5 - PythongefeiView Answer on Stackoverflow
Solution 6 - PythonvatsaView Answer on Stackoverflow
Solution 7 - PythonFastSolutionsView Answer on Stackoverflow
Solution 8 - PythonSomeGuyOnAComputerView Answer on Stackoverflow
Solution 9 - Pythonkrazykode101View Answer on Stackoverflow
Solution 10 - PythonviviView Answer on Stackoverflow