setting up emacs to run python using elpy

  • Last Update :
  • Techknowledgy :

The quickest way to start is just to open a Python file in Emacs – ‘python-mode’ has a nice feature-set as is – see below for details.,python-x extends python-mode with features inspired by EmacsSpeaksStatistics and targeted to interactive code evaluation with an inferior Python process.,The Python pstats module provides an interactive tool when a script from a regular shell in Emacs; it can also be used in a Python shell running in Emacs.,For more basic configuration, so that ‘info-lookup-symbol’ searches the Python docs, customize Info as below.

The built-in python.el has support for imenu code navigation. By default it uses a tree structure for navigation which presents only top level definitions that can then be ‘drilled in to’. If you prefer a flat menu then you can add the following snippet (retrieved from GitHub) to your init file:

(defun python - imenu - use - flat - index()
   (setq imenu - create - index - function # 'python-imenu-create-flat-index))

      (add - hook 'python-mode-hook
         # 'python-imenu-use-flat-index)

pycoverage generates reports using coverage and provides a minor mode for displaying coverage by overriding ‘linum-mode’. Alternatively, a Flycheck checker could be used to display coverage as below.

  (flycheck - define - checker python - pycoverage "A Python test coverage checker using the pycoverage tool.

     See `'.

This works after pytest has run by marking lines missing
coverage (as reported by pytest) as flycheck issues.  If the code
was updated after pytest was run then nothing is reported.
    ("python" "-c"
      (mapconcat 'identity
                  "import sys"
                  (format "sys.path.insert(0, '%scov2emacs')" (file-name-directory (locate-library "pycoverage")))
                  "from cov2emacslib.__init__ import main"
     "--compile-mode" "--python-file" source-original)
    :error-patterns ((warning line-start (file-name) ":" line ":" (message) line-end))
    :modes (python-mode)))

In Emacs’s native python-mode, use:

  (setq python - shell - interpreter "ipython"
     python - shell - interpreter - args "-i --simple-prompt --InteractiveShell.display_page=True")

For more basic configuration, so that ‘info-lookup-symbol’ searches the Python docs, customize Info as below.

(require 'info-look)

   (info - lookup - add - help: mode 'python-mode: regexp "[[:alnum:]_]+": doc - spec '(("(python)Index" nil "")))

When using Emacs 24.1 on Mac OS X compiled via homebrew, the python-shell always used US-ASCII as encoding. To fix it I used:

(setenv "LC_CTYPE"
(setenv "LC_ALL"
(setenv "LANG"

Suggestion : 2

Elpy is an extension for the Emacs text editor to work with Python projects. This documentation explains how to use Elpy to work on Python project using Emacs, but it does not aim to be an introduction to either Emacs or Python.,You can read a quick tour of Emacs, or read the built-in tutorial by running C-h t in the editor. That is, you hold down the control key and hit h (the canonical help key in Emacs), release both, and hit t (for tutorial).,Show the current Elpy configuration, point out possible problems, and provide a quick interface to relevant customization options.,For Python, you can read the basic tutorial. If you already know Python, you should check out some best practices.

(use - package elpy: ensure t: init(elpy - enable))
(use - package elpy: ensure t: defer t: init(advice - add 'python-mode :before '
   elpy - enable))
sudo apt install elpa - elpy
(elpy - enable)
sudo apt install python3 - jedi black python3 - autopep8 yapf3 python3 - yapf
(require 'package)
   (add - to - list 'package-archives
      '("melpa-stable" . ""))

Suggestion : 3

Elpy is an Emacs package to bring powerful Python editing to Emacs. It combines and configures a number of other packages, both written in Emacs Lisp as well as Python. Elpy is fully documented at Readthedocs.,Once installed, Elpy will automatically provide code completion, syntax error highlighting and code hinting (in the modeline) for python files. Elpy offers a lot of features, but the following keybindings should be enough to get started:,In the meantime, I will keep an eye on the PRs to integrate any fix and/or new features proposed, but I will definitely not be able to treat the issues in a satisfying manner. If you are stuck with an issue, please have a look at the documentation, there is a lot of answers there. @gfederix also made some bug fixing in his fork, so you can try using this version of Elpy.,I find myself (@galaunay, current maintainer), unable to maintain Elpy at the moment, and probably for a while. If you are interested in getting involved in Elpy, please contact me by mail, the project definitely needs you !

(use - package elpy: ensure t: init(elpy - enable))

Suggestion : 4

Extend the list of packages to install, by adding elpy:,If, on startup, packages can't be found, try M-x pacakge-refresh-contents.,eldoc error: (error (error Elpy necessitates the 'virtualenv' python package, please install it with pip install virtualenv)), Instantly share code, notes, and snippets.

Create a user-level initialisation file init.el:

touch.emacs.d / init.el

Add the following to the init.el file:

init.el-- - Emacs configuration

function key as Meta

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

(require 'package)
   (add - to - list 'package-archives                                                                                                        
      '("elpy" . ""))   
      (add - to - list 'package-archives
         '("melpa" . "") t)

         ;; activate all packages(package - initialize)

         ;; fetch the list of packages available(unless package - archive - contents(package - refresh - contents))

         ;; define list of packages to install(defvar myPackages '(better-defaults
            material - theme exec - path - from - shell elpy pyenv - mode))

      ;; install all packages in list(mapc # '(lambda (package)
         (unless(package - installed - p package)
            (package - install package))) myPackages)

   ;; Use shell 's $PATH
   (exec - path - from - shell - copy - env "PATH")

   ;; init.el ends here

Add the following to the init.el file, after the INSTALL PACKAGES section:

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

(setq inhibit - startup - message t);;
hide the startup message
   (load - theme 'material t)           ;; load material theme
      (global - linum - mode t);; enable line numbers globally(setq linum - format "%4d \u2502 ");; format line number spacing;; Allow hash to be entered(global - set - key(kbd "M-3")
         '(lambda () (interactive) (insert "#")))

and add the commands to enable elpy and its interactions:

(elpy - enable)
(pyenv - mode)
(setq python - shell - interpreter "ipython"
   python - shell - interpreter - args "-i --simple-prompt")

To help elpy out, provide autocompletion and syntax checking/linting, install the following at the command-line (e.g. with pip):


Suggestion : 5

I recently downloaded Emacs 25.2 with the dependencies on Windows 10. I'm attempting to run python script using elpy. I created an init file including -

(require 'package)
   (add - to - list 'package-archives
      '("melpa" . ""))
      (package - initialize)
      (elpy - enable)

After failing, I also manually installed elpy - After running the first half of the init (above) ->

M - x package - refresh - contents RET
M - x package - install RET elpy RET
  1. Nor can I compile basic code -
print("Hello world")