4

Preface:

I've been dabbling with python programming for a mere few months now. Previously, I spent most of my programming time using C# with Visual Studio. In the Python world, I've been using PyCharm for my projects. It has excellent code-completion (much like the IntelliSense I'm used to) and a nice level of customizability. However, at the end of the day, I want something a little bit lighter and less clunky. I have seen videos of developers using Emacs with Python and I'm quite interested in learning it.

Setup:

  • Python 3.6.0 is installed.
  • I have downloaded emacs-25.1-x86_64-w64-mingw32.zip from the Emacs website.
  • I have created a folder C:\emacs and extracted the zip file to this location
  • I have identified the Emacs home folder as C:\Users\joshu\AppData\Roaming\.emacs.d
  • I have created an init.el file in this directory since one did not exist

Python Integration:

Now, this is where I'm struggling. I'm still extremely new to the software so it's a bit hard to piece together information from the internet. Using this tutorial I have developed a baseline init.el file. Due to my reputation points, I cannot include a third link here. The Gist containing my init.el file is commented below.

When I open Emacs and open a python file, editing works. However, I'm running into the issues outlined below:

  1. C-c C-c returns an error. It seems to be a relatively wide-spread error upon Googling. By simply typing C-c C-c again, the shell will appear with the code being executed; however, it gives me the line python.el: native completion setup failed

Edit: The error is Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native’ was t and "python3" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally.

  1. Python autocomplete doesn't seem to be working properly. When using PyCharm, I get a much, much more robust offering of completions. In the case of Emacs, if I begin typing something such as from tkint it will not suggest tkinter. Similarly, if I start typing from math import a it will not suggest abs. I'm not sure at all how to configure Python autocompletion. I've read a few things about using pip install jedi and so on, but I haven't done any of that.

I realize this may be just as much a python question as an Emacs question in some regards. Any insight is appreciated.

9

4 Answers 4

2

This seems to be bug in emacs and its fixed in this commit.

As npostavs mentioned, you can use

(with-eval-after-load 'python
  (defun python-shell-completion-native-try ()
    "Return non-nil if can trigger native completion."
    (let ((python-shell-completion-native-enable t)
          (python-shell-completion-native-output-timeout
           python-shell-completion-native-try-output-timeout))
      (python-shell-completion-native-get-completions
       (get-buffer-process (current-buffer))
       nil "_"))))

You can read entire discussion here.

1

I use the following:

(setq
 python-shell-interpreter "ipython3"
 python-shell-interpreter-args "--simple-prompt --pprint")

For python 3.6 I found jedi with company to be the most reliable but others may have different experiences. "mypath" below are the paths to packages I've created that I want autocomplete.

(use-package company-jedi      
 :config
  (setq jedi:environment-virtualenv (list (expand-file-name "~/.emacs.d/.python-environments/")))
  (add-hook 'python-mode-hook 'jedi:setup)
  (setq jedi:complete-on-dot t)
  (setq jedi:use-shortcuts t)
  (setq jedi:server-args
  '("--sys-path" "mypath1"
    "--sys-path" "mypath2"))

  (defun config/enable-company-jedi ()
    (add-to-list 'company-backends 'company-jedi))
  (add-hook 'python-mode-hook 'config/enable-company-jedi))
0

In Ubuntu 16.04, I added python-setuptools in addition to python3-setuptools. Problem disappeared.

(I'm using Emacs 25.3.2, from the stable kelleyk PPA; Spacemacs user)

0

Regarding programming Python in emacs, be sure to check out LSP. It is a wonderful initiative that is well maintained and supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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