1

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.

4
  • In Notepad++ try Encoding -> Convert to UTF8 and also Convert to UTF8 without BOM and try again in Emacs.
    – Artyer
    Commented Nov 2, 2016 at 19:14
  • Thanks for the suggestion. I tried both, but got the same result. (When I opened it in Notepad++ initially, "Encode in UTF-8" was already selected. I tried converting anyway.)
    – user7106363
    Commented Nov 2, 2016 at 19:30
  • Maybe stackoverflow.com/questions/3366499/… can help. Has lots of votes and similar adjustment to settings in notepad++ Commented Nov 2, 2016 at 19:36
  • Replacing tabs with spaces did not fix it. HOWEVER, by showing the whitespace symbols, I found that the end of line was both a carriage return and a line feed. Removing the carriage return fixes the problem. I guess I will have to find and replace \r in all my scripts in Notepad++ (unless there's a faster way).
    – user7106363
    Commented Nov 2, 2016 at 20:07

1 Answer 1

2

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)