2

I set up an environment to write Python 3 Code in my emacs (25.2.2) using python-mode and Elpy on Ubuntu 18.04. Strangely every time I use Python --> Send --> Execute Buffer the program is executed twice. It doesn't matter whether I use the mapped key (F6) or go via the menu.

My code in the relevant parts (it's not doing much so far, I'm mostly trying to learn using emacs as an IDE):

import sys

class MyClass:
    def __init__(self, argv):
        # Does something...    

    def __call__(self, x):
        print("Call was called") # But is never called

if __name__ == "__main__":
    print('~~~~Start~~~~')
    # Does not much more
    print('~~~~End~~~~')

The result looks like that:

>>> ~~~~Start~~~~
~~~~End~~~~
>>> ~~~~Start~~~~
~~~~End~~~~

Disclaimer: I'm very new to emacs, so please excuse if the question doesn't have enough information. I'm willing to provide - just need to know how.

Edit (1): Tried to run a simple Hello world program:

if __name__ == '__main__':
    print("Hello World")

Same result:

>>> Hello World
>>> Hello World
Qohelet
  • 165
  • 1
  • 9
  • Does this happen when using `emacs -Q`? I believe a minimal setup would require the following commands `(package-initialize) (require 'package) (package-refresh-contents) (pyvenv-activate "/path/to/venv/") (elpy-enable)` – Lorem Ipsum Jan 25 '19 at 14:17
  • You might also check the `elpy-config`. – Lorem Ipsum Jan 25 '19 at 14:19
  • It's strange your code is executing `if __name__ == "__main__":` block. Did you try an even simpler code? Did it execute twice? You can execute your code with `C-c C-c` (or `elpy-shell-send-buffer`). And if you post your actual code, it will be easier for people to help. – adl Jan 25 '19 at 22:01
  • @LoremIpsum - what to check in the elpy-config? – Qohelet Jan 26 '19 at 18:35
  • @adl - Added simpler code, same result. The code is not the problem. Using your commands doesn't give me any output, just two empty lines. – Qohelet Jan 28 '19 at 06:13
  • @Qohelet `elpy-config` shows what virtual environment is active, where the remote process call (RPC) calls, and other such details. It might give you a hint that something is not set up correctly. Check that the paths are what you expect them to be. – Lorem Ipsum Jan 28 '19 at 13:13
  • 1
    Please add which OS you are using – npostavs Jan 28 '19 at 14:19
  • @npostavs - added: Ubuntu 18.04. – Qohelet Jan 29 '19 at 08:55
  • Another Update. It's python-mode causing the problems – Qohelet Feb 01 '19 at 15:35
  • At this stage it will be useful to add/post the elisp code used in your init.el file - if you use elpy, you may start the **ipython** interpreter which is a better one. The ```if __name__ == '__main__':``` usually executes from Terminal. – Ian Feb 03 '19 at 15:33

0 Answers0