user7106363
user7106363

Reputation:

Python Script from Notepad++ Error in Emacs

I have a large library of python scripts that I wrote in Notepad++ and Pycharm, and I would like to start running them with Emacs. However, something about these files is causing a syntax error in Emacs.

As a minimum working example, I saved the following in Notepad++:

print('hello world')

Then, I open it in Emacs 22.3.1, and go to Python --> Start Interpreter. I type C-c C-c, and I get the following error in the interpreter screen:

File "/tmp/py110681cL", line 1
    print('hello world')
^
SyntaxError: invalid syntax

This makes it look like there is an indentation it is reading at the beginning of the line, but I do not see it in the Emacs text editor window. I tried cleaning up whitespace to no avail.

If I create a new file in Emacs and manually type the same code, it runs just fine following this procedure.

I'd rather not manually retype all of my pre-existing scripts, so how can I remove this phantom space? Google was not helpful...

UPDATE: Notepad++ was representing my new lines with [CR][LF]. Removing the carriage return in Notepad++ allowed it to run in Emacs.

Upvotes: 1

Views: 157

Answers (1)

Artyer
Artyer

Reputation: 40836

The problem seems to be that notepad++ is using \r\n (CARRIAGE RETURN + LINE FEED) to represent newlines, and Emacs interprets the \r as an invalid indent (as it is whitespace).

To prevent this from happening in the future, you can go to Settings -> Preferences... -> New Document -> Format (Line ending) and select Unix/OSX. \r\n is the Windows way of representing newlines, and it is \n in Unix/OSX/Linux. (Old Mac is just \r).

To ammend the existing files, you can do:

find /path/to/py/files -type f -iname '*.py' -exec sed $'s/\r$//' "{}" +;

(Finds all .py files in /path/to/py/files and executes sed $'s/\r$//' on them (Replace all \rs which are followed by end of lines with nothing)

Upvotes: 2

Related Questions