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