Installing Emacs for Python Development on Major Platforms#

Emacs is a powerful, customizable text editor that is popular among developers for its flexibility and extensive features. It can be configured to support almost any programming language, including Python, using plugins and extensions. While Emacs has a steep learning curve, its customizable nature makes it a favorite among power users. This guide covers the installation process for Emacs on Windows, macOS, and Linux, and provides steps to set up Python development with essential plugins like elpy, lsp-mode, and blacken for code formatting.

Installing Emacs on Windows#

Method 1: Using winget#

  • Overview: winget is a command-line package manager for Windows provided by Microsoft.

  • Installation:

    • Open Command Prompt or PowerShell and run:

      winget install GNU.Emacs
      
    • This command downloads and installs the latest version of Emacs.

  • Verify Installation:

    • Launch Emacs from the Start Menu or by running:

      emacs
      

Method 2: Using Chocolatey (choco)#

  • Overview: Chocolatey is a popular package manager for Windows.

  • Installation:

    • Install Chocolatey by following the instructions on Chocolatey’s website.

    • After choco is installed, open Command Prompt or PowerShell and run:

      choco install emacs
      
  • Verify Installation:

    • Open Emacs from the Start Menu or by running:

      emacs
      

Method 3: Using the Installer#

  • Overview: Download the Emacs installer directly from the official GNU Emacs website.

  • Installation:

    • Visit the download page and select the Windows installer.

    • Run the installer and follow the setup wizard instructions.

  • Verify Installation:

    • Launch Emacs from the desktop shortcut or the Start Menu.

Installing Emacs on macOS#

Method 1: Using brew#

  • Overview: brew is a package manager for macOS that simplifies software installations.

  • Installation:

    • Install Homebrew by running the following in Terminal:

      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
    • Install Emacs with:

      brew install emacs
      
  • Verify Installation:

    • Launch Emacs from the Applications folder or using the terminal:

      emacs
      

Method 2: Using the Installer#

  • Overview: Download the Emacs .dmg file from the official website.

  • Installation:

    • Open the downloaded .dmg file and drag the Emacs icon into the Applications folder.

  • Verify Installation:

    • Open Emacs from the Applications folder or by typing emacs in the terminal.

Installing Emacs on Linux#

Emacs can be installed on various Linux distributions using package managers like apt, dnf, or yum, or by building it from source.

Method 1: Using apt for Debian/Ubuntu#

  • Overview: apt is the package manager for Debian-based systems like Ubuntu.

  • Installation:

    • Open Terminal and run:

      sudo apt update
      sudo apt install emacs
      
  • Verify Installation:

    • Launch Emacs using:

      emacs
      

Method 2: Using dnf for Fedora#

  • Overview: dnf is the package manager for Fedora.

  • Installation:

    • Open Terminal and run:

      sudo dnf install emacs
      
  • Verify Installation:

    • Open Emacs with:

      emacs
      

Method 3: Building from Source#

  • Overview: If you want the latest version of Emacs, you can build it from source.

  • Installation:

    • Download the latest version from the GNU Emacs website.

    • Extract the tarball:

      tar -xzf emacs-*.tar.gz
      
    • Change into the directory and compile:

      cd emacs-*
      ./configure
      make
      sudo make install
      
  • Verify Installation:

    • Launch Emacs using:

      emacs
      

Adding Python Support to Emacs#

To use Emacs effectively for Python development, you can install and configure a few essential plugins.

1. Installing elpy for Python Development#

  • Overview: elpy is a popular Emacs package for Python development, providing autocompletion, code navigation, and more.

  • Installation:

    • Open Emacs and run:

      M-x package-refresh-contents
      M-x package-install RET elpy RET
      
  • Enable elpy:

    • Add the following to your Emacs configuration file (~/.emacs or ~/.emacs.d/init.el):

      (elpy-enable)
      
  • Restart Emacs for the changes to take effect.

2. Configuring lsp-mode for Advanced Features#

  • Overview: lsp-mode is a Language Server Protocol package that provides advanced code analysis and completion for Python.

  • Installation:

    • Open Emacs and install lsp-mode:

      M-x package-install RET lsp-mode RET
      
    • Install the Python language server with:

      pip install 'python-lsp-server[all]'
      
  • Enable lsp-mode for Python:

    • Add the following to your Emacs configuration:

      (add-hook 'python-mode-hook #'lsp)
      

3. Installing blacken for Code Formatting#

  • Overview: blacken is an Emacs package that integrates the black formatter for automatically formatting Python code.

  • Installation:

    • Open Emacs and install blacken:

      M-x package-install RET blacken RET
      
    • Add the following to your Emacs configuration:

      (add-hook 'python-mode-hook 'blacken-mode)
      
    • Ensure black is installed:

      pip install black
      

4. Running Python Scripts in Emacs#

  • Overview: Emacs allows you to run Python scripts directly within the editor.

  • Steps:

    • Open a Python file (.py) and press:

      M-x run-python
      
    • To execute a script, use:

      C-c C-c
      

      This will run the current Python buffer.

Summary#

Emacs is a powerful and flexible editor that, when combined with plugins like elpy, lsp-mode, and blacken, can serve as a fully-featured IDE for Python development. It can be installed using winget, choco, brew, or package managers like apt and dnf on Linux, making it accessible on all major platforms. With its extensive customization capabilities, Emacs is a great choice for developers who prefer a highly configurable environment.