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.
4Could you please be more specific? "IDE" has become a buzzword and grab bag for all kinds of features.– wasamasaMar 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 DoMar 1, 2015 at 11:38
2A 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
1I'm voting to close this question as off-topic because it is too broad.– DrewMar 1, 2015 at 15:39
5IDEs may be useful, but "to program you first need an IDE" is just silly. Plenty of good code got written before there were IDEs.– offby1Jun 12, 2015 at 23:36
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 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.)
2Thank 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
2Thanks @Jorgen. I use
elpydaily and have a quick question about your answer, when you say that "
M-x pdbcan run the Python debugger", how is this exactly? I have a Python buffer, and when I do
M-x pdbI get a prompt in the minibuffer that says
"Run pdb (like this): pdb my_file.py". When I press
RETI 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.1Mar 1, 2015 at 22:04
1Pdb comes with Python. I have no idea why it would not be found, I am afraid. :-( Mar 2, 2015 at 4:48
pdbis not found, but
pythonis found locally, then you can run pdb using
M-x pdbthen "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.– aadcgSep 25, 2019 at 11:03
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
(setq-default dotspacemacs-configuration-layers '(python))
out of curiosity where exactly in .spacemacs should i add this? is the very top ok? before (defun dotspacemacs/layers () ... ? Aug 1, 2017 at 21:12
There is a list called
dotspacemacs-configuration-layers. Add it there :) Aug 14, 2017 at 12:50
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.
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,…
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.
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…).
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
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
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?– thinwybkSep 25, 2019 at 9:47
I use purpose-mode. This allows you to define a pretty robust environment.
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.
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 )
- 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
checkout --track debug-layer/debug-layer
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 trepan3kas it does not come pre-installed with your python distribution.
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 )
- 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
clyton's answer above, the git repo should be github.com/CeleritasCelery/debug-layer. The PR for this is currently open at github.com/syl20bnr/spacemacs/pull/9246 May 12, 2018 at 19:36
(I can't comment owing to lack of reputation) The above has been merged into the develop branch of spacemacs, so step one can be ignored. May 13, 2018 at 22:11
realgud is very good for debugging. Feb 16, 2020 at 23:20