As a full-stack developer and Linux enthusiast, I rely on Python daily for a variety of projects. From web development to machine learning, Python‘s simplicity yet versatility makes it my language of choice. However, to be maximally productive in Python, having the right tools is essential.
One such indispensable tool is Emacs – the highly customizable, extensible text editor that has stood the test of time. With the right configuration, Emacs transforms into a powerful Python IDE providing autocompletion, on-the-fly error checking, debugging and much more.
In this comprehensive guide, I‘ll walk you through configuring Emacs for Python step-by-step:
Prerequisites
Before we dive in, ensure the following dependencies are installed on your system:
pip3 install jedi autopep8 flake8 black yapf importmagic ipython
These provide essential functionality like linting, formatting, autocompletion etc. inside Emacs.
Additionally, ensure you have the latest Emacs version installed. I recommend building from source to get the newest features.
With the dependencies out of the way, let‘s move on to tailoring Emacs specifically for Python.
Customizing the Initialization File
When Emacs starts up, it executes commands from the initialization file ~/.emacs.d/init.el. By adding the appropriate code here, we can tweak Emacs to our liking.
Setting up Package Management
We begin by setting up MELPA (Milkypostman’s Emacs Lisp Package Archive) to easily install additional Emacs packages:
(require ‘package)
(add-to-list ‘package-archives
‘("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
This adds MELPA as a package source. We‘ll leverage this later to download packages.
UI Customization
Next, let‘s make some UI customizations by adding:
(tool-bar-mode -1) ; disable toolbar
(toggle-scroll-bar -1) ; disable scrollbar
(setq inhibit-startup-message t) ; hide startup message
(load-theme ‘tango-dark) ; prettify with color scheme
(global-linum-mode t) ; enable line numbers globally
This removes unnecessary clutter and applies a popular color scheme. The end result is a cleaner interface resembling modern editors.

Feel free to tweak as per your preferences – the sky is the limit when customizing Emacs!
Installing Packages
Now we utilize package management to install plugins that enhance Python development:
(defvar my-packages
‘(
lsp-pyright ; code intelligence
pyvenv ; Python virtual env
elpy ; Emacs Lisp Python Environment
company ; autocompletion
lsp-ui ; LSP user interface
)
)
(mapc #‘(lambda (package)
(unless (package-installed-p package)
(package-install package)))
my-packages)
This batch installs incredibly useful packages like elpy, company and pyright in one go thanks to MELPA setup earlier. Feel free to tweak this list based on your needs.
Let‘s briefly understand the purpose of each package:
- elpy – Provides IDE features for Python. Auto-completion, error highlighting, code navigation/refactoring, debugger integration etc.
- company – Sophisticated auto-completion that integrates well with elpy.
- lsp-pyright – Language server protocol support for type checking and other IDE functionality.
- pyvenv – Python virtual environment integration.
- lsp-ui – Adds handy UI enhancements for language servers.
And that concludes Emacs configuration! Let‘s see it all come together by writing some Python code.
Writing Python Code in Emacs
Fire up Emacs and open a python file. Let‘s walk through some of the excellent features we now have for Python thanks to our configuration.
Code Completion
Start typing a variable/function/class name. You‘ll immediately see autocompletion popups listing possibilities to complete the code. This works for built-ins as well as code defined within the project itself:

Keep typing to filter further and press <tab> to complete the selected item. The sophistication of this completions system powered by company and lsp blows my mind.
Type Checking and Linting
Emacs analyzes your code in real time. It underlines errors and warnings as you type which you can hover over to view details:

Here declaring an incorrect variable type is caught immediately thanks to pyright integration. The red squiggly line indicates an outright error. Orange lines denote warnings.
I‘ve found this instant feedback invaluable to nip issues in the bud while coding vs trying to debug at the end.
Documentation Lookup
Simply move your cursor over any symbol and hit C-c C-d to view relevant documentation inline:

The documentation displays definitions, types and docstrings without having to leave context. Very handy!
Python Refactoring/Navigation
Emacs makes navigating and editing code a breeze:
C-M-ato move functions up/downC-M-kto kill lines without copying to clipboardC-S-wto copy regions to clipboardM-.to jump to function definitions
This is just the tip of the iceberg of what‘s possible. Look into excellent plugins like expand-region and magit to take it even further.
Integrated Debugging
Zero config debugging is built right in via M-x pdb. Set breakpoints and step through code as you would in an IDE:

The tight integration with debugging and ability to inspect data in real time helps accelerate development.
And much more for linting, virtual environments, running tests etc. As you can see, Emacs transforms into a full-featured Python IDE catering to all your needs once correctly configured.
Final Thoughts
Emacs outshines traditional IDEs by being infinitely customizable. Configuring it specifically for Python as seen in this guide unlocks next-level productivity.
The fluid editing experience, intelligent coding assistance and frictionless environment in Emacs has made me far more efficient in Python. I hope you find the same by following this guide.
Do share any Emacs packages/tips you find useful for Python in the comments!


