This small program runs well in Terminal :

mac2011% cat bug.py
print(__name__ == "__main__")
#if True:
if __name__ == "__main__":
    print("not main")
mac2011% python bug.py
mac2011% python --version
Python 3.6.9 :: Anaconda, Inc.

In Emacs:

mac2011% Emacs --version
GNU Emacs 26.3
mac2011% Emacs -q bug.py

the file opens in (Python ElDoc) mode.

Python menu

Start interpreter (C-c C-p)

Eval buffer (C-c C-c)

Python 3.6.9 |Anaconda, Inc.| (default,     Jul 30 2019, 13:42:17) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or     "license" for more information.
>>> python.el: native completion setup loaded
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alba/Documents/projets/plot time series/bug.py", line 5
SyntaxError: invalid syntax

Replacing if __name__ == "__main__": by if True:, there is no more error.

Replacing "__main__" by "__main_", there is no error.

Can you reproduce the facts?

Do you have any explanation?

How to repair?

Same with Python 3.7.6.

My emacs comes from Emacs for Mac OS X.

Same with emacs 28.0.50 that I have just built.

  • May reproduce it with GNU Emacs 26.3 (build 1, i686-pc-linux-gnu, GTK+ Version 3.14.5) of 2019-09-04. Please consider a bug-report. – Andreas Röhler Jan 10 '20 at 8:23
  • I have filed a bug report with GNU. Meanwhile, I have noticed this: even the first line of the script, that should print True ou False, is not executed. Indeed, the error will be the same, whatever the python file content. – Pierre ALBARÈDE Jan 10 '20 at 10:46
  • Unless the if line is commented out. – Pierre ALBARÈDE Jan 10 '20 at 10:57
  • This equivalent syntax works : print("main") if __name__ == '__main__' else print("not main") – Pierre ALBARÈDE Jan 10 '20 at 11:11
  • print("main") if __name__ == "__main__" else print("not main") does not work today with Emacs 27.1 – mankoff May 13 at 14:25

Use C-u C-c C-c.

See python-shell-buffer-substring function docstring:

When optional argument NOMAIN is non-nil everything under an if __name__ == '__main__' block will be removed.

And python-shell-send-buffer:

When optional argument SEND-MAIN is non-nil, allow execution of code inside blocks delimited by if __name__== '__main__':. When called interactively SEND-MAIN defaults to nil, unless it’s called with prefix argument.

So the code actually sent by C-c C-c is:

print(__name__ == "__main__")
#if True:

    print("not main")
  • 1
    Nice find, but the feature is pretty nasty IMO. – NickD Jan 10 '20 at 15:28
  • @NickD It allows to surpass some stuff if intended. Currently, the bug seems causing the opposite. – Andreas Röhler Jan 10 '20 at 15:38
  • @NickD This behavior was added by git.savannah.gnu.org/cgit/emacs.git/commit/… with really brief comment. Maybe it should do the opposite - strip if-name-main block when invoked with prefix argument. – muffinmad Jan 10 '20 at 15:44
  • True. Admitting that Emacs has to modify the code, for a reason that is not yet completely clear to me, at least it should do it cleanly, i. e., remove the full block if [elif...] else and maybe issue a warning. This might finally be a bug. – Pierre ALBARÈDE Jan 10 '20 at 18:51
  • Reading the Git page, I notice a loop hole in the filtering, where the alternative syntax print("main") if __name__ == '__main__' else print("not main") goes through. – Pierre ALBARÈDE Jan 10 '20 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.