Python Development Emacs Environment
Emacs Lisp Python Shell HTML Ruby CSS Other
Permalink
Failed to load latest commit information.
Pymacs #11, which-function-mode sluggish Dec 9, 2015
completion Urs Fleisch 2016-01-20 Fix test_pycomplete merged Jan 31, 2016
devel Extended executes again Nov 24, 2016
doc Doku updated Feb 10, 2016
extensions extensions/.nosearch added Apr 21, 2016
obsolete Rope bindings moved from pdee-bindings.el into pdee-init.el Dec 11, 2011
python-libs Starting to develop unittesting Nov 22, 2011
scripts Further editing on package.py Apr 10, 2011
snippets/django Some yasnippets for Django templates (html-mode). Aug 2, 2011
test unused `py--imenu-create-index-new-intern' removed Nov 24, 2016
.gitignore Remove redundant all-travis-ci.sh Feb 4, 2016
.travis.yml travis again Jul 1, 2015
CONTRIBUTORS Added Bastian to the CONTRIBUTORS list Jun 15, 2011
COPYING added license Jun 18, 2010
CREDITS #23, py-pylint-run uses incorrect filename for remote files Aug 5, 2016
ChangeLog Updated readme and changelog with the new feature Jul 29, 2011
EXTENSIONS keep just one PDEE main directory Aug 15, 2012
INSTALL keep just one PDEE main directory Aug 15, 2012
INSTALL-INFO-FILES info-lookup-symbol, lp:1281950 Feb 19, 2014
IPYTHON keep just one PDEE main directory Aug 15, 2012
NEWS py-up forms alias provided Oct 14, 2016
PROBLEMS.org `py-split-window-on-execute-p' renamed `py-split-window-on-execute' Nov 8, 2014
README-PYMACS.org README-PYMACS.org added Nov 29, 2014
README.md README.md added Nov 24, 2016
README.org Provide Richard Stanton's comment at README.org/troubleshooting Mar 16, 2015
README_AUTOPAIR.org py-ert-function-tests.el rewrite Mar 10, 2015
README_DEVEL.org README_DEVEL.org extended Jul 23, 2015
ToDo.org Navigation - major rewrite Oct 31, 2016
all-travis-ci.sh Remove redundant all-travis-ci.sh Feb 4, 2016
auto-complete-pycomplete.el keep just one PDEE main directory Aug 15, 2012
byte-compile-directory.sh Some more minor fixes WRT lexical scope Nov 12, 2016
company-pycomplete.el keep just one PDEE main directory Aug 15, 2012
index.docbook keep just one PDEE main directory Aug 15, 2012
makeElpa renamed: Makefile -> makeElpa Dec 6, 2011
pdee-completion.el Don't add hooks that operate on non-existent variables Dec 9, 2011
pdee-editing.el fixes issue #29 Feb 20, 2013
pdee-init.el wrong-coloring, lp:1315186 May 6, 2014
pdee-pkg.el Add package metadata files for ELPA packkage format and Marmalade add… Nov 26, 2011
pycomplete+.el keep just one PDEE main directory Aug 15, 2012
pyflakespep8.py #11, which-function-mode sluggish Dec 9, 2015
pymacs.el lexical-binding: don't use add-to-list Nov 17, 2016
python-abbrev-propose.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-auto-fill.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-backward-forms.el Navigation args cleanup Nov 16, 2016
python-components-beginning-position-forms.el py--beginning-... forms, `py--end-base' fixed Nov 23, 2016
python-components-booleans-beginning-forms.el py--beginning-... forms, `py--end-base' fixed Nov 23, 2016
python-components-booleans-end-forms.el Still more fixes WRT lexical-binding Nov 17, 2016
python-components-bounds-forms.el Some cleanup Jun 24, 2015
python-components-close-forms.el python-components-close-forms.el added Nov 12, 2016
python-components-comment.el Still more fixes WRT lexical-binding Nov 17, 2016
python-components-completion_flymake.el remove obsolete, risky forms Jun 29, 2013
python-components-copy-forms.el Fixes WRT lexical scope Nov 4, 2016
python-components-copy.el Add missing files Nov 4, 2016
python-components-create-menu.el Menu updated Jul 1, 2015
python-components-dedicated-shells.el keep just one PDEE main directory Aug 15, 2012
python-components-delete-forms.el `py--match-paren-forward' fixed Nov 16, 2016
python-components-delete.el email updated Jul 1, 2015
python-components-edit.el `py-edit-docstring' minor fix Nov 24, 2016
python-components-electric.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-end-forms.el Some cleanup Jun 24, 2015
python-components-end-position-forms.el Fixes WRT lexical scope Nov 4, 2016
python-components-exec-forms.el Fixes WRT lexical scope Nov 4, 2016
python-components-execute-file.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-execute-region.el Add missing files Nov 4, 2016
python-components-execute.el Minor argument fixes Nov 24, 2016
python-components-extensions.el Minor argument fixes Nov 24, 2016
python-components-fast-complete.el lexical-binding, hand over more arguments Nov 20, 2016
python-components-fast-forms.el lexical-binding, fast process arguments Nov 18, 2016
python-components-foot.el lexical-binding: don't use add-to-list Nov 17, 2016
python-components-forms-code.el Fixes WRT lexical scope Nov 4, 2016
python-components-forms.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-forward-forms.el Still more fixes WRT lexical-binding Nov 17, 2016
python-components-help.el lexical-binding: more minor fixes Nov 17, 2016
python-components-hide-show.el lexical-binding: more minor fixes Nov 17, 2016
python-components-imenu.el Minor argument fixes Nov 24, 2016
python-components-intern.el py--beginning-... forms, `py--end-base' fixed Nov 23, 2016
python-components-kill-forms.el Remove duplicates Nov 16, 2016
python-components-mark-forms.el `py--match-paren-forward' fixed Nov 16, 2016
python-components-menu.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-misc-forms.el `py-beginning-of-for-block', lp:1367424 Jan 27, 2015
python-components-mode.el `py-edit-docstring' minor fix Nov 24, 2016
python-components-move.el py--beginning-... forms, `py--end-base' fixed Nov 23, 2016
python-components-named-shells.el Fixes WRT lexical scope Nov 4, 2016
python-components-narrow.el Add missing files Nov 4, 2016
python-components-paragraph.el lexical-binding: more minor fixes Nov 17, 2016
python-components-pdb.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-section-forms.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-send.el Still more fixes WRT lexical-binding Nov 17, 2016
python-components-shell-complete.el lexical-binding: don't use add-to-list Nov 17, 2016
python-components-shell-menu.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-shift-forms.el lexical-binding: more minor fixes Nov 17, 2016
python-components-switches.el `py--indent-line-intern' dont shadow `py-indent-offset' Nov 16, 2016
python-components-unified-completion.el keep just one PDEE main directory Aug 15, 2012
python-components-up-down.el Still more fixes WRT lexical-binding Nov 17, 2016
python-components-versioned.el Some more minor fixes WRT lexical scope Nov 12, 2016
python-components-virtualenv.el lexical-binding: don't use add-to-list Nov 17, 2016
python-dedicated-shells.el keep just one PDEE main directory Aug 15, 2012
python-extended-executes.el Extended executes again Nov 24, 2016
python-mode-fixed-examples.py keep just one PDEE main directory Aug 15, 2012
python-named-shells.el keep just one PDEE main directory Aug 15, 2012
python-shell-complete.el keep just one PDEE main directory Aug 15, 2012
python-shell-install-example-buffer.el keep just one PDEE main directory Aug 15, 2012
run-travis-ci.sh Some more minor fixes WRT lexical scope Nov 12, 2016
setup.py #11, which-function-mode sluggish Dec 9, 2015
thingatpt-python-expressions.el keep just one PDEE main directory Aug 15, 2012

README.md

Initialize

Put the following into your initialization file:

(setq py-install-directory "PATH/TO/PYTHON-MODE/") (add-to-list 'load-path py-install-directory) (require 'python-mode)

Selecting a Python shell:

Customize default Python shell as `py-shell-name'

py-shell-name' might be an installed default executable as shell commandtype' would display, but also a PATH/TO/EXECUTABLE

If different flavours of Python are installed, customize py-python-command - python2 py-python3-command py-ipython-command

py-python-command-args py-python3-command-args py-ipython-command-args

You might run code by another installed (I)Python-version than default.

Normally this would happen if a shebang in buffer specifies it. Shebang precedes default py-shell-name.

In case you will run code by default-shell, ignoring shebang, set `py-force-py-shell-name-p' to. This might be done also via menu Python/.../Switches

Another way to enforce a different shell is specifiyng a path --interactively C-u prompts for that-- or using commands which end in version like `py-execute-statement-python3' See menu Python/Interpreter/Other/Ignoring Defaults

Invoking an (I)Python interactive shell

Commands related to a specific shell start with it's name as `ipython-complete'. Open an installed shell by

M-x SHELL RET

This calls py-shell with its default arguments

(py-shell &optional ARGPROMPT DEDICATED SHELL BUFFER-NAME FAST-PROCESS EXCEPTION-BUFFER)

if DEDICATED is set to `t', shell will get an unique name.

Install a local shell by evaluating

(defun MY-LOCAL-SHELL () (interactive) (py-shell nil DEDICATED PATH-TO-LOCAL-SHELL))

* Invoking versioned Python shells in interactive mode:

M-x py-shell RET ==> "*Python*"
M-x python RET ==> "*Python*"
M-x ipython RET ==> "*IPython*"
M-x python3 RET ==> "*Python3*" 

When Path/To/Executable is given
C-u python RET ==> "*PTE Python*"

Prefix conventions

Most python-mode.el commands start with prefix `py-'

`M-x py- TAB' displays a list of them in completion-buffer. See also commands list delivered in directory doc.

List virtualenv related M-x virtualenv- TAB' resp. Pymacs commandsM-x pymacs-'

Execution code

Python code might be processed by an

  • interactive Python shell (DEFAULT)
  • non-interactive Python (`py-fast-process-p') The latter, while design for large output, seems more reliable - see also PROBLEMS.org.

Both processes might run in

  • session, i.e. start from possible previous state (DEFAULT)
  • dedicated, (`py-dedicated-process-p') run in separate process

There is also

  • python-mode-v5-behavior

Checks

Access is provided to a couple of known checkers like Flake8, pep8, pylint Need to be installed for example calling "pip install pep8" from a shell command-line Get flycheck from https://www.flycheck.org

Displaying Output

py-execute-...'-commands arrive in buffer created by py-shell'. It's name is composed WRT to Python version used, it's path etc.

Result of commands ending "-fast" arrives in `py-fast-output-buffer'

Window management

Variables in question:

** py-keep-windows-configuration

Default is nil. When non-nil, it takes precedence over py-split-window-on-execute' andpy-switch-buffers-on-execute-p' settings

Useful, if a pre-designed set of python-shells/buffers should be maintained

** py-split-window-on-execute

When non-nil split windows according to value set.

Default is 'just-two: when code is send to interpreter, split screen into source-code buffer and current py-shell result. Other buffers will be hidden that way.

When set to `t', python-mode tries to reuse existing windows and will split only if needed.

With 'always, results will displayed in a new window.

** py-switch-buffers-on-execute-p

Put focus into the output buffer: this will display buffer in any case

Completion

At the end of a word TAB by default calls completion.

Auto-completion should be available via

(require 'auto-complete-config) (ac-config-default)

or for company:

(autoload 'company-mode "company" nil t)

There is also a Pymacs based auto-completion mode see README-PYMACS.org

Either use Pymacs and company-mode' withpycomplete.el' etc. --load stuff from folder completion--

or switch py-auto-completion-mode-p' - which relies on py-shell-complete', i.e. calls the current Python process.

Extern tools like jedi-server/mode should work too.

Moving

Beside common moves like defun',statement', block called via py-end-of-..., py-beginning-... specific Python-mode edits are delivered:

py-expression' andpy-partial-expression'.

Statement below is considered composed of two `py-expression'

a = ['spam', 'eggs', 100, 1234] || |_________________________|

Assigment operator and all inside comments is ignored.

`py-partial-expression' would match six sections

a = ['spam', 'eggs', 100, 1234] || |_____| |_____| || |_| |_____________________________|

When traversing code, py-partial-expression' climbs down and up all levels encountered, i.e. at opening[' py-expression' would return ['spam', 'eggs', 100, 1234], while one char behind at'' it yields `'spam','

  • py-sexp-function, When set, it's value is called instead of forward-sexp',backward-sexp Choices are py-partial-expression, py-expression, default nil

Filling

Customize boolean `py-set-fill-column-p'

If t', enables use Python specificfill-column' according to

`py-docstring-fill-column', default is 72

and `py-comment-fill-column, default is 79

Comment- and docstring settings might be disabled by any non-integer value, which means: do not use a different value of `fill-column' than emacs-wide

Python and IPython

Start IPython shell after loading python-mode via M-x ipython, not from plain shell.

Executing code through IPython should work as with regular Python, also getting completions from. However, with IPython, it feels a demi-second slower.

Troubleshooting

Start with Emacs -Q from the directory where python-mode.el lives. Open python-mode.el and evaluate it.

Open a file with ending ".py".

M-x python RET

a regular Python-shell should appear

M-x IPython RET

an IPython-shell should be opened

** pdb doesn't work at Windows Richard Stanton commented:

Running M-x pdb doesn't work on my Windows machine, primarily because Windows (at least using the default shell) doesn't automatically know what to do when you give it a .py command at the command line.

For example, here's the suggested command when I run pdb on a file c:\projects/run.py:

c:/python27/Lib/pdb.py run.py

If I accept this, I get an error "Spawning child process: Invalid argument"

A work-around to get it to work is to replace the suggested command with

c:\python27\python -i c:/python27/Lib/pdb.py c:/projects/run.py

(note that I not only have to add the python command, but also fully qualify the script file, since otherwise it complains it can't find the file).