Elpy

Elpy provides an immediately usable IDE for python in Emacs . Initially i used this. Later moved to default python mode.

verify if pip3 is installed.

$pip3 –version

If not install pip3 using the command

$sudo apt install python3-pip

Now verify that pip3 is installed.

$pip3 –version

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

$pip3 install jedi autopep8 flake8 ipython importmagic yapf

It is much easier to install elpy from OS provider than melpa

$sudo apt install elpa-elpy

But don’t do it. It causes errors. Use melpa-stable.

Don’t use the virtualenv package from os. Use pip3 to install pyvenv

https://github.com/jorgenschaefer/pyvenv ↗

The following is needed

https://virtualenvwrapper.readthedocs.io/en/latest/install.html#python-versions ↗

export WORKON_HOME=$HOME/.virtualenvs

export PROJECT_HOME=$HOME/Devel

source /usr/local/bin/virtualenvwrapper.sh

After editing it, reload the startup file (e.g., run source ~/.bashrc).

The above step will not work if python python path is not set properly.

Then use https://stackoverflow.com/questions/44970069/python-could-not-import-the-module-virtualenvwrapper-hook-loader ↗

The right way is to copy the following code in your ~/.bashrc.

VIRTUALENVWRAPPER_PYTHON=$(which python3)

create a venv, replicate my python3 and modules in it and finally have emacs load that venv.

Now ensure that packages related to material theme and better-defaults are installed from melpa as it is much easier that way. Add the following lines to your .emacs.d/init.el and restart emacs.

(require ‘package)

(add-to-list ‘package-archives

‘(“melpa-stable” . “ https://stable.melpa.org/packages/ ↗ ”))

M-x package-install RET elpy RET

For a system without python 2, the package python-is-python3 package must be installed using apt.

For the error message, “.emacs.d/elpy/rpc-venv/bin/python: No module named pip” or if your M-x eply-config does not see all the installed tools like jedi, black etc, issue the following command.

M-x elpy-rpc-reinstall-virtualenv

The above command solves the above issues.

prabu@onepc:~$ cat .emacs.d/init.el

;; Added by Package.el. This must come before configurations of

;; installed packages. Don’t delete this line. If you don’t want it,

;; just comment it out by adding a semicolon to the start of the line.

;; You may delete these explanatory comments.

(package-initialize)

(add-to-list ’load-path “/usr/local/elisp”)

(custom-set-variables

;; custom-set-variables was added by Custom.

;; If you edit it by hand, you could mess it up, so be careful.

;; Your init file should contain only one such instance.

;; If there is more than one, they won’t work right.

;; ‘(custom-enabled-themes (quote (manoj-dark)))

;; ‘(custom-safe-themes

;; (quote

;; (“62b36dfb65f112784e3a63d5226a86e75458adba516a899dacde1068bc3a30e6” default)))

‘(package-selected-packages (quote (material-theme org-plus-contrib org))))

(custom-set-faces

;; custom-set-faces was added by Custom.

;; If you edit it by hand, you could mess it up, so be careful.

;; Your init file should contain only one such instance.

;; If there is more than one, they won’t work right.

)

(setq inhibit-startup-message t)

(load-theme ‘material t)

(global-linum-mode t)

(load “better-defaults”)

(load “elpy”)

(elpy-enable)

(setq python-shell-interpreter “ipython”

python-shell-interpreter-args “-i –simple-prompt”)

Sources: https://elpy.readthedocs.io/en/latest/introduction.html ↗

https://linuxhint.com/configuring_emacs_python/ ↗

A sample complete working init.el file is given below

;; Added by Package.el. This must come before configurations of

;; installed packages. Don’t delete this line. If you don’t want it,

;; just comment it out by adding a semicolon to the start of the line.

;; You may delete these explanatory comments.

(package-initialize)

(add-to-list ’load-path “/usr/local/elisp”)

(custom-set-variables

;; custom-set-variables was added by Custom.

;; If you edit it by hand, you could mess it up, so be careful.

;; Your init file should contain only one such instance.

;; If there is more than one, they won’t work right.

;; ‘(custom-enabled-themes (quote (manoj-dark)))

;; ‘(custom-safe-themes

;; (quote

;; (“62b36dfb65f112784e3a63d5226a86e75458adba516a899dacde1068bc3a30e6” default)))

;;’(package-selected-packages (quote (material-theme org-plus-contrib org)))

)

(custom-set-faces

;; custom-set-faces was added by Custom.

;; If you edit it by hand, you could mess it up, so be careful.

;; Your init file should contain only one such instance.

;; If there is more than one, they won’t work right.

)

;;(load-theme ‘manoj-dark) ;;To load the builtin manoj-dark theme, just uncomment this line

;; https://github.com/rougier/elegant-emacs ↗ ;;To make emacs more elegant, checkout the above website

;; https://github.com/jmdeldin/ir-black-theme.el/blob/master/ir-black-theme.el ↗ ;;yet another theme

;; https://stackoverflow.com/questions/2079095/how-to-modularize-an-emacs-configuration/2079146 ↗

(defconst user-init-dir

(cond ((boundp ‘user-emacs-directory)

user-emacs-directory)

((boundp ‘user-init-directory)

user-init-directory)

(t “~/.emacs.d/”)))

(defun load-user-file (file)

(interactive “f”)

“Load a file in current user’s configuration directory”

(load-file (expand-file-name file user-init-dir)))

(load-user-file “beancount.el”)

(load-user-file “beancountcustom.el”)

(load-user-file “org.el”)

;;(add-to-list ’load-path “~/.emacs.d/lisp”)

;; Change the tab behaviour. As per pg175 of O’Reilly book “Learning Emacs” 3rd Edition.

;; To make Tab-width consistent with all OS and vi editor

(setq-default tab-width 4)

;; Emacs inserts spaces for indentation rather than tab characters

(setq-default indent-tabs-mode nil)

;; The below section allows external file handlers when inside emacs dired mode.

;; https://emacs.stackexchange.com/questions/60284/how-to-open-image-files-with-an-external-application-in-dired ↗

;; Did not go with the below option using openwith package. Instead opted for dired-x

;;(when (require ‘openwith nil ’noerror)

;; (setq openwith-associations

;; (list

;; (list (openwith-make-extension-regexp ‘(“doc” “docx” “rtf”)) “word” ‘(file))

;; ))

;;(openwith-mode 1))

;; https://www.gnu.org/software/emacs/manual/html_mono/dired-x.html ↗

(add-hook ‘dired-load-hook

(lambda ()

(load “dired-x”)

;; Set dired-x global variables here. For example:

;; (setq dired-guess-shell-gnutar “gtar”)

;; (setq dired-x-hands-off-my-keys nil)

))

(add-hook ‘dired-mode-hook

(lambda ()

;; Set dired-x buffer-local variables here. For example:

;; (dired-omit-mode 1)

))

;; the below option opens pdf files with zathura and picture files with picture viewer on pressing ! against the filename.

;; https://emacs.stackexchange.com/questions/3698/how-can-i-add-default-shell-commands-for-various-file-types ↗

(setq dired-guess-shell-alist-user

(list

(list “\\.pdf$” “zathura”);; fixed rule

(list “\\.jpg$” “feh”);; fixed rule

(list “\\.jpeg$” “feh”);; fixed rule

(list “\\.png$” “feh”);; fixed rule

(list “\\.tif$” “feh”);; fixed rule

(list “\\.jif$” “feh”);; fixed rule

(list “\\.tiff$” “feh”);; fixed rule

(list “\\.html$” “firefox”);; fixed rule

(list “\\.htm$” “firefox”);; fixed rule

))

;; https://stackoverflow.com/questions/64360/how-to-copy-text-from-emacs-to-another-application-on-linux ↗

(setq select-enable-clipboard t)

(setq interprogram-paste-function ‘x-cut-buffer-or-selection-value)

;; https://web-mode.org/ ↗

(load-file “~/local-gits/web-mode/web-mode.el”)

(require ‘web-mode)

(add-to-list ‘auto-mode-alist ‘("\\.phtml\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.tpl\\.php\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.[agj]sp\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.as[cp]x\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.erb\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.mustache\\’" . web-mode))

(add-to-list ‘auto-mode-alist ‘("\\.djhtml\\’" . web-mode))

;; Using web-mode for editing plain HTML files can be done this way

(add-to-list ‘auto-mode-alist ‘("\\.html?\\’" . web-mode))

;; https://emacs.stackexchange.com/questions/55367/how-to-render-html-in-web-mode ↗

(with-eval-after-load ‘web-mode

(define-key web-mode-map (kbd “C-c C-v”) ‘browse-url-of-buffer))

;; To quickly type or paste in emacs instead of standard emacs startpage

(setq-default inhibit-startup-screen t)

(setq inhibit-splash-screen t)

(setq inhibit-startup-message t)

(setq initial-scratch-message “”)

;; (setq initial-buffer-choice “/home/prabu/newfile.txt”)

(setq initial-major-mode ‘fundamental-mode)

;; The below lines needs to uncommented for installing any package from melpa. Material theme was loaded from here..

;;(require ‘package)

;;(add-to-list ‘package-archives

;; ‘(“melpa” . “ http://melpa.org/packages/ ↗ ”) t)

;;(package-initialize)

;;(package-refresh-contents)

(load-theme ‘material t)

;;Most of the below options taken from https://linuxhint.com/configuring_emacs_python/ ↗ . Refer to elpy_howto for actual details.

(add-to-list ’load-path “/home/prabu/local-gits/better-defaults”)

(require ‘better-defaults)

;; the below line disabled as it’s defined elsewhere

;;(global-linum-mode t)

(elpy-enable)

;; use ipython instead of default python shell

(setq python-shell-interpreter “ipython”

python-shell-interpreter-args “-i –simple-prompt”)

;; Configure flymake for Python

;; https://docs.pylint.org/en/1.6.0/ide-integration.html ↗

(when (load “flymake” t)

(defun flymake-pylint-init ()

(let* ((temp-file (flymake-init-create-temp-buffer-copy

‘flymake-create-temp-inplace))

(local-file (file-relative-name

temp-file

(file-name-directory buffer-file-name))))

(list “epylint” (list local-file))))

(add-to-list ‘flymake-allowed-file-name-masks

‘("\\.py\\’" flymake-pylint-init)))

;; Set as a minor mode for Python

(add-hook ‘python-mode-hook ‘(lambda () (flymake-mode)))


© Prabu Anand K 2020-2026