Skip to content

Instantly share code, notes, and snippets.

@widdowquinn
Last active April 27, 2023 17:36
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save widdowquinn/987164746810f4e8b88402628b387d39 to your computer and use it in GitHub Desktop.
Save widdowquinn/987164746810f4e8b88402628b387d39 to your computer and use it in GitHub Desktop.
Turning Emacs into a Python IDE

Turning emacs into a Python IDE

## Create a new config/initialisation file

Create a user-level initialisation file init.el:

touch .emacs.d/init.el

Install packages

Add the following to the init.el file:

;; init.el --- Emacs configuration

;; Set OSX function key as Meta


;; INSTALL PACKAGES
;; --------------------------------------

(require 'package)
(add-to-list 'package-archives                                                                                                        
             '("elpy" . "http://jorgenschaefer.github.io/packages/"))   
(add-to-list 'package-archives
       '("melpa" . "https://melpa.org/packages/") 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

Here, better-defaults is a set of minor changes to Emacs defaults, and material-theme is an Emacs display theme.

We have to use the GitHub repo for elpy, or else an error is thrown with a variable xref-location1

If, on startup, packages can't be found, try M-x pacakge-refresh-contents.

Basic customisation

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

;; BASIC CUSTOMIZATION
;; --------------------------------------

(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 "#")))

This loads the material theme, turns off the startup message, and enables line numbering in all buffers.

Turn on elpy for Python development

Extend the list of packages to install, by adding elpy:

;; define list of packages to install
(defvar myPackages
  '(better-defaults
    material-theme
    elpy))

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):

jedi
autopep8
flake8
ipython
importmagic
yapf

Using the virtualenv Python

M-x pyvenv-activate

Then enter the name/location of the venv

@evan886
Copy link

evan886 commented Oct 16, 2018

good

@xiaohaijin
Copy link

nice

@necronomican
Copy link

If I would want to use it with an anaconda env, what do I do?
I tried to put the path to the conda env but the following eldoc error occurs.

eldoc error: (error (error Elpy necessitates the 'virtualenv' python package, please install it with pip install virtualenv))

@widdowquinn
Copy link
Author

@necronomican - you may want to use anaconda-mode (https://github.com/pythonic-emacs/anaconda-mode) instead. I haven't used it myself, but it looks more appropriate for you.

Copy link

ghost commented Aug 27, 2022

This is extremely helpful thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment