1

I do a lot of R coding on Emacs – on Windows – using the wonderful ESS package. What I particularly like is that I can:

  • start an R session, and then send lines or regions of the script I'm working on to that session
  • send the whole script to be evaluated
  • switch back and forth between R session and script editing

ESS also provides some documentation when inputting an R function (for example which arguments it requires) and some autocompletion features. But these are less important to me than the above three.

Can you suggest an Emacs mode for working with python scripts, similar to ESS in the three points above? (I have an Anaconda Windows installation.)

I've explored elpy, but it's extremely slow (often completely freezes Emacs) and doesn't seem to have the possibility of sending lines to the interpreter. It has the right functionality but can be too slow in its base configuration. Probably it tries to do too much; for example it gives information about imported packages but frankly I don't need that. No project-management help is needed either.

I've heard of anaconda mode but it doesn't seem to provide interaction with an interpreter: only autocompletion and documentation. I've also heard of python mode but haven't understood if it provides interaction with a python interpreter. Finally, Jedi is a total mystery, and EIN doesn't seem to be designed for scripting.

It seems that with python part of the problem is also the choice of interpreter, if I understood correctly. In this case, which python interpreter would be best, to achieve something similar to the R+ESS seamless interaction?

Most related questions on StackExchange are 4–6 years old; presumably a lot has changed in the meantime.

Thank you all.

2
  • 2
    There are plans to add python support to ESS in the not so distant future. Somewhere in the range of 6-12 months I would say. – VitoshKa Apr 4 '20 at 19:32
  • That's great news. Looking forward to it. – pglpm Apr 5 '20 at 12:22
1

I managed to set up something close to my requirements above. Here it is, for people who may have my same needs.

Let me stress that such requirements are very subjective, so the set-up below is not meant as a fit-all solution. It specifically addresses Windows users.

The setup involves the python distribution and Emacs's configuration:

Python distribution

I had to use the one from python.org. I installed several packages through pip – including numpy, jupyter, panda – and the install went smoothly.

I see most people recommend the Anaconda distribution, which is surely great owing to the possibility of working in "conda environments". But this possibility turned out to be a problem for me.

From what I saw, even if you only have the base conda environment, you still need to activate it before using any python executables. This means that Emacs cannot simply call, say, ipython.exe: it needs first to call activate base and then to call ipython. Alternatively you could open a command or cygwin prompt, activate base there, and then start Emacs from that prompt. This would be a hassle for me. I don't need conda environments, so I can do fine with the python.org distribution. (If you do need conda environments, maybe the remarks above may help you).

Emacs configuration

In my question (now edited) I was too harsh regarding elpy. It works smoothly if you unselect some of its modules.

So if you, like me, don't need elpy's full documentation and project-management capabilities, I recommend the following settings, through Emacs's customize-apropos:

  • Elpy Modules: I activate only:

    • Inline code completion
    • Show function signatures
    • Show the virtualenv in the mode line
  • elpy-project-root is set to nil. This is useful if you have many files and subdirectories, which otherwise elpy would scan, making Emacs freeze from time to time.

  • Elpy Shell Starting Directory is set to Current directory, for the same reason.

  • Elpy Shell Use Project Root is toggled off, for the same reason.

You can also specify the location of your python distribution in the customizable variable Python Shell Exec Path. In my case I set it to c:/ProgramData/Python38-32/ and c:/ProgramData/Python38-32/Scripts/. This way python doesn't need to be put in you PATH environment variable.

Finally, for the python shell interpreter I customized

  • Python Shell Interpreter to ipython

and

  • Python Shell Interpreter Args to -i --simple-prompt

according to elpy's documentation. I tried the other alternatives given there too, and all worked fine. I can also see plots made with matplotlib.

Elpy provides several commands for sending lines, regions, or the whole buffer to the interpreter. See the documentation here.

I hope this set-up will help other Emacs-python-Windows users.

If you know of workarounds to Anaconda's "activate" problem, please do share them in the comments!

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.