70

Emacs is an excellent editor - however it is said "to program you first need an IDE" - so how would you build an IDE in the extensively customizable Emacs for PYTHON with all the features of modern IDE. I want to be able to effectively debug, run and compile, and manage code. Thus far I have read up on the subject I have installed the Elpy package but am unsure of what else need to know and do to in order make this possible.

9
  • 4
    Could you please be more specific? "IDE" has become a buzzword and grab bag for all kinds of features.
    – wasamasa
    Mar 1, 2015 at 8:41
  • I guess you are new to Emacs and you may want to look at my Helm and Helm Projectile guide.
    – Tu Do
    Mar 1, 2015 at 11:38
  • 2
    A google search for "emacs python ide" gives a fair number of hits. Could you clarify what you've tried already and what is not yet satisfactory?
    – Dan
    Mar 1, 2015 at 11:57
  • 1
    I'm voting to close this question as off-topic because it is too broad.
    – Drew
    Mar 1, 2015 at 15:39
  • 5
    IDEs may be useful, but "to program you first need an IDE" is just silly. Plenty of good code got written before there were IDEs.
    – offby1
    Jun 12, 2015 at 23:36

5 Answers 5

68

The default Python mode in Emacs has a number of features, including code completion based on the inferior Python shell, so that can be used as a basic IDE without any added packages. You can use other core Emacs functions with Python, too. For example, M-x compile/M-x recompile can be used to run tests. M-x pdb can run the Python debugger, and it integrates with Emacs' gud environment so you actually get inline breakpoints.

To expand on that, there are three major packages for Emacs that try to implement a more featureful Python IDE (in alphabetical order):

They all provide roughly the same features:

  • Code completion using either auto-complete or company-mode
  • Code navigation, that is go to definition/usages
  • Function signature display using ElDoc
  • Documentation introspection on the symbol at point
  • Syntax checks using flymake or flycheck
  • And some of them (at least Elpy) have basic refactoring support

Beyond these, Emacs can also help you with a lot of workflows, but it does not prescribe any single one. So the next steps depend a lot on how you are using Python. The best approach here would be to figure out a good Python workflow and then try to find Emacs packages that help you specifically with the steps on your workflow.

For example, there are multiple packages to work with virtualenvs (Elpy, which you mention, comes with pyvenv) which you should likely use. Emacs-Jedi has an accompanying mode for code browsing which can be useful. If you are using IPython a lot, there's an IPython Notebook for Emacs. There are special support modes for Django development etc. – you can go to http://melpa.org/ and search for Python to get some ideas.

Outside of Python, you might benefit from some extra Emacs packages that help with development in general. Projectile is a generic package for project management which gives you a number of interesting features, like searching in a project or moving between tests and implementations. If it hasn't been installed as a dependency from the IDE package of your choice yet, you can also use YASnippet which provides intelligent snippets for code generation. The default snippet collection has a large number of snippets for Python, too.

But again, the main IDE packages above will get you a sane environment – the next steps greatly depend on your specific workflows.

(Full disclosure: I am the author of Elpy, so take that into account when reading the above.)

5
  • 2
    Thank you very much for such a comprehensive response, it has proved extensively useful in ways that countless articles have not. Mar 1, 2015 at 10:20
  • 2
    Thanks @Jorgen. I use elpy daily and have a quick question about your answer, when you say that "M-x pdb can run the Python debugger", how is this exactly? I have a Python buffer, and when I do M-x pdb I get a prompt in the minibuffer that says "Run pdb (like this): pdb my_file.py". When I press RET I get "ImportError: No module named pdb.pdb: 'pdb is not a package" This is with the latest PYthon 3.4 after just upgrading pdb with pip, which gives me: $ pip freeze | grep pdb => pdb==0.1 Mar 1, 2015 at 22:04
  • 1
    Pdb comes with Python. I have no idea why it would not be found, I am afraid. :-( Mar 2, 2015 at 4:48
  • @JorgenSchäfer if pdb is not found, but python is found locally, then you can run pdb using M-x pdb then "Run pdb (like this): python -m pdb my_file.py` Jan 7, 2018 at 2:30
  • I also find the sphinx-doc package useful to write docstrings.
    – aadcg
    Sep 25, 2019 at 11:03
17

I would encourage evil-emacs users to try spacemacs.

It has a brilliant python-layer, see the details here: https://github.com/syl20bnr/spacemacs/tree/master/layers/!lang/python

As of now, it includes:

  • Auto-completion using anaconda-mode
  • Code Navigation using anaconda-mode
  • Documentation Lookup using anaconda-mode and pylookup
  • Test Runner using nose.el or pytest
  • Virtual Environment using pyvenv
  • Semantic mode is enabled
  • Django support via pony-mode

Installation is ridiculously easy, just add the following line to your ~/.spacemacs:

(setq-default dotspacemacs-configuration-layers '(python))
2
  • out of curiosity where exactly in .spacemacs should i add this? is the very top ok? before (defun dotspacemacs/layers () ... ?
    – user391339
    Aug 1, 2017 at 21:12
  • There is a list called dotspacemacs-configuration-layers. Add it there :) Aug 14, 2017 at 12:50
8

Here is a comprehensive guide: http://wikemacs.org/wiki/Python It lists ready-to-use solutions (the Prelude starter kit, Spacemacs, solutions for data science,…), big useful packages (Elpy), as well as every package you need to improve your environment. Here's an overview.

Refactoring

This is the most "IDE-specific" feature. Emacs as some possibilities at it thanks to the Rope python library. It turns out the easiest way to install and use is the emacs-traad package, in MELPA. It features, among others:

  • change a method signature: add/remove an argument, with cross-project refactoring (of course),
  • rename anything,
  • find definitions, …

Rope is also included in Elpy.

Simpler code manipulation

We have simpler tools to help refactoring or in code manipulation: http://wikemacs.org/wiki/Python#Other_Python_.22refactoring.22_tools

For example, Projectile is great (see other answers), we also have a tool to add decorators, add/remove/edit arguments, rename/copy/comment/kill a method, a tool for code generation,…

Code navigation

We can navigate as an IDE with tags and cscope: find the functions calling this one, find occurences, etc.

Emacs also has great packages like imenu (helm-imenu), helm-swoop (interactive grep in a buffer), emacs-helm-ag (interactive silver searcher in a project), etc.

Running tests

It is Elpy that provides good support to run tests (with the django runner, elpy or another one). We can even run only the unit test we are currently in: very handy ! So there is a Hydra to help with that (cicle between the errors, switch to the python prompt…).

Make support

This is an Emacs package not specific to Python. But I like to be able to run a make target from anywhere in the project and to choose the make command with completion. link

Dealing with the indentation

There's a little package, indent-tools, that gives commands to work directly with an indented block: move to the next-previous-child-parent block of indentation, indent/deindent/comment/copy/kill/fold the current-block, etc. Perfect for yaml, and also for Python. Doc and gif demo

Django support

see Django. Elpy supports the django runner. See elpy django doc. django-mode (in melpa) has templates highlighting, quick commands, management commands completion,…

Also important: great Git, Github and Gitlab support

magithub can create github PRs, we can fetch issues and insert their reference in commit messages, magit is great,… see http://wikemacs.org/wiki/Git

1
  • Especially in larger and/or scuttered projects refactoring is super important not to break something. Does someone know how to setup emacs-traad with Spacemacs?
    – thinwybk
    Sep 25, 2019 at 9:47
7

I use purpose-mode. This allows you to define a pretty robust environment.

Using elpy, pydoc, and neotree I have a pretty robust environment (in the sense that windows don't do strange things at errors). This can also work pretty well with gud-pdb for debugging.

emacs-purpose setup for python with neotree, python-mode, pydoc, and inferior python (through elpy)

2

The answers here have provided most of the important features that spacemacs provides but there was always one feature that I could not find about -- DEBUGGING(I did not find any decent debugging tools that go on well with spacemacs until very recently)

So how did I setup a debug workflow for my spacemacs?

There is a feature branch under development that uses realgud.el package to provide a close to IDE like debug experience in spacemacs : https://github.com/CeleritasCelery/spacemacs All you have to do is pull this branch into your .emacs.d directory and make some changes in your ~/.emacs.d/layers/+lang/python/packages.el file

Detailed Steps to setup spacemacs for debug ( Take a deep breath )

  1. Type the below commands in your terminal one by one. This will add CeleritaCelery's spacemacs changes in a separate branch named 'debug-layer'. This means that if anything gets messed up you can always checkout the master branch and voila! your original spacemacs is back!

remote add debug-layer https://github.com/CeleritasCelery/spacemacs fetch debug-layer checkout --track debug-layer/debug-layer branch -a

  1. Now open the ~/.emacs.d/layers/+lang/python/packages.el and add the below function to the end of the file ;; configure trepan3k as the python debugger to be used with realgud (defun python/pre-init-realgud() (dolist (mode '(anaconda-mode)) ;; bind trepan3k with anaconda mode (spacemacs|add-realgud-debugger mode "trepan3k"))) This function uses the trepan3k debugger as the python debugger. You can change the value to pdb or ipdb or whichever debugger you like. If you plan on using trepan3k make sure you install it with pip sudo pip install trepan3k as it does not come pre-installed with your python distribution.

  2. Now open your .spacemacs file and 'debug' layer inside dotspacemacs-configuration-layers like this dotspacemacs-configuration-layers '( (python :variables python-enable-yapf-format-on-save t flycheck-disabled-checkers '(python-flake8) flycheck-checker 'python-pylint ) debug )

  3. Then restart spacemacs and open any python file You can start debugging by typing M-x realgud:trepan3k

This will start the debug session and you can add,delete breakpoints using realgud keybindings explained here : https://github.com/realgud/realgud#source-window-commands

You're done!

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.