EzDevInfo.com

python-mode

Vim python-mode. PyLint, Rope, Pydoc, breakpoints from box.

Turning on linum-mode when in python/c mode

I want to turn on linum mode (M-x linum-mode) automatically with python and c mode. I add the following code in .emacs, but it doesn't seem to work.

(defun my-c-mode-common-hook ()
  (line-number-mode 1))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(defun my-python-mode-common-hook ()
  (line-number-mode 1))
(add-hook 'python-mode-common-hook 'my-python-mode-common-hook)

What might be wrong?


Source: (StackOverflow)

Apply automatic pep8 fixes from QuickFix window

Background:

I'm using the (fantastic) Vim plugin python-mode, which includes the pep8 linter. The :PyLint command runs all linters and opens errors in a QuickFix window.

Problem:

Now, let's assume I'm only using the pep8 linter, and I have a QuickFix window full of errors. I'd like to step through each of these errors and apply an automatic fix (with something like autopep8). The autopep8 tool is fantastic, but it makes mistakes. Ideally, I'd like to be able to supervise each fix in Vim (apply fix, check, move to next fix).

My current approach is to run autopep8 on my Python file, diff the results, then repair any bad changes:

$ autopep8 --in-place spam.py
$ git difftool spam.py  # check edits in gVim, write to file
$ git commit spam.py -m "Fix bad PEP8 formatting"

However, this approach ruins my undo history, and seems needlessly complex. Is there a better way?

Question:

Is there any way to automatically apply pep8 fixes (when available) to pep8 errors within the QuickFix window?


Source: (StackOverflow)

Advertisements

Enable auto-indent in python-mode (python.el) in Emacs 24?

I've recently declared emacs bankruptcy and in rebuilding my config switched from the old python-mode.el to the built-in python.el.

One thing that's I'm missing is the old behaviour of auto-indenting to the correct level when hitting RET. Is there any way to re-enable this?


Source: (StackOverflow)

Determine path to source code of current mode in Emacs 23

I'm trying out a new Python mode for Emacs 23, but I'm not sure how to tell if I'm using the new mode (source code located at ~/.elisp/python.el) or the bundled python.el mode.

Is there a way to find out where the current (or any active) mode was loaded from? C-h m does not seem to provide that information, and I don't know where else to look.


Source: (StackOverflow)

How can I clear Vim's interface of the PyLint marks?

I've configured python-mode to check manually. So I type :PyLint and it checks my code, showing the "QuickFix" window and some marks at the side. I can subsequently close the QuickFix window by typing :onlyin the other window or so, but how can I clear the side marks?


Source: (StackOverflow)

Emacs python-mode: Keyboard shortcuts for pdb step-by-step debugging

I was wondering if there is a way to associate:

  • n RET (next)
  • p RET (previous)
  • c RET (continue)
  • C-x SPC RET (set/clear breakpoint)

with function keys F1-F12 or other keyboard shortcuts. The idea is to emulate the keyboard shortcuts that other IDEs have for debugging (e.g. Visual Studio, MATLAB, etc.).

Is this already supported by python-mode? Are there any Emacs modes that can be used to complement python-mode for debugging purposes?


Source: (StackOverflow)

Evaling files in directory

Is there any way to get python-mode to eval all files in a directory (or at least all the files I'm importing from)?

When I work on a file that imports from another file in the same directory, I have to kill and then re-create the inferior python process in order to pick up changes made in the dependent files.


Source: (StackOverflow)

Emacs: split window size in python-mode

I set split-width-threshold to nil, so on execution the python shell will appear in a split window below. However, the shell always occupies half of the frame. Is it possible to adjust the size (automatically on execution)?

The py-split-windows-on-execute-function seems to only accept two options (split-window-vertically and split-window-horizontally).

Emacs version: 24.3


Source: (StackOverflow)

emacs python-mode: designate which python shell to send commands to

A follow up on this question - if I have two python shells running in emacs, how can I designate which shell commands from each python script buffer is sent to? I suspect it has something to do with python-buffer and python-set-proc but setting these variables to the name of the shell is apparently not the solution.

Edit: actually since I am using python-mode rather than loveshack python, it probably does not have to do with python-buffer and python-set-proc.


Source: (StackOverflow)

Emacs Unbind a Mode's KeyBinding [duplicate]

This question already has an answer here:

I've created a custom key binding macro as follows:

(global-set-key (kbd "C-C C-c") "\C-a\C- \C-n\M-w\C-y")

The problem is that C-c C-c is defined for python-send-buffer in python-mode. So my macro works for all modes except python-mode. I am assuming that python-mode is evaluated after my init file, so it overwrites that keybinding.

I tried unsetting C-c C-c using (eval-after-load "python-mode") and using global-unset-key but that doesn't work. C-c C-c in python is always mapping to python-send-buffer.

How can I completely disable Python's C-c C-c, and use my macro instead?

I am using Emacs 24.2.1.


Source: (StackOverflow)

How can I access directory-local variables in my major mode hooks?

I have defined a .dir-locals.el file with the following content:

((python-mode . ((cr/virtualenv-name . "saas"))))

In my .emacs I have the following function to retrieve this value and provide a virtualenv path:

(defun cr/virtualenv ()
  (cond (cr/virtualenv-name (format "%s/%s" virtualenv-base cr/virtualenv-name))
        ((getenv "EMACS_VIRTUAL_ENV") (getenv "EMACS_VIRTUAL_ENV"))
        (t "~/.emacs.d/python")))

Finally, in my python-mode-hook list, I have this hook function:

(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)

(defun cr/python-mode-shell-setup ()
  (message "virtualenv-name is %s" cr/virtualenv-name)
  (let ((python-base (cr/virtualenv)))
    (cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
           (setq python-python-command (concat python-base "/bin/ipython"))
           (setq py-python-command (concat python-base "/bin/ipython"))
           (setq py-python-command-args '( "-colors" "NoColor")))
          (t
           (setq python-python-command (concat python-base "/bin/python"))
           (setq py-python-command (concat python-base "/bin/python"))
           (setq py-python-command-args nil)))))

When I open a new python file, the message logged by cr/python-mode-shell-setup indicates that cr/virtualenv-name is nil. However, when I C-h v the name, I get "saas" instead.

Obviously there's a load order issue here; is there a way to have my mode hook statements respond to directory-local variables?


Source: (StackOverflow)

How to get Aquamacs to run code in ipython

I am using Aquamacs on OS X lion and trying to use the latest python-mode.el to edit and run my python code. I can get the ipython shell to open by typing C-!, but if I run either C-c C-c or C-c | the python code runs in a new Python buffer without ipython. I have tried a number of fixes. Here are the python specific parts of my .emacs file:

(when (featurep 'python) (unload-feature 'python t))
;; add custom libs
(add-to-list 'load-path "~/tools/emacs")
;; Set up pylab 
(setq py-install-directory "/Users/stringham/tools/emacs/")
(require 'python-mode)
(setq py-shell-name "/usr/local/bin/ipython")
(setq py-python-command "/usr/local/bin/ipython")

Has anyone had success in using ipython with Aquamacs?


Source: (StackOverflow)

Using multiple Python shells in Emacs 'python-mode' with Python or IPython

Is there a way to force a new instance of python-shell while running Emacs? It would be convenient when working on multiple projects with separate working directories (and different sets of modules).

Any attempt to invoke python-shell will only pull up the current instance.


Source: (StackOverflow)

Syntastic and Python-mode together?

I have installed python-mode in VIM. But I also have Syntastic installed. Since both do syntax checking, is there going to be a conflict? How can I turn off Syntastic for Python files?

Thanks for any help


Source: (StackOverflow)

improper exiting from indentation in emacs python-mode

I am using Emacs python-mode. I invoke it using this in my .emacs

(add-to-list 'load-path "~/emacs/python-mode.el-6.0.3/")
(autoload 'python-mode "python-mode" "Python Mode." t)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
(require 'python-mode)
(add-hook 'python-mode-hook
      (lambda ()
    (set-variable 'py-indent-offset 4)
    ;(set-variable 'py-smart-indentation nil)
    (set-variable 'indent-tabs-mode nil)
    (define-key py-mode-map (kbd "RET") 'newline-and-indent)
    ;(define-key py-mode-map [tab] 'yas/expand)
    ;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)
    ))

It generally indents okay, in that if I wrote:

if condition:

and then pressed Return, it would properly put the cursor in the indented newline. The problem is that it doesn't exit out of indentation correctly. In other systems, when I make a new line in the body of the indented clause (like the if statement body) and press Backspace, it jumps one level out in indentation, instead of backspacing. For example, if I had:

if condition: 
  statement1
  statement2

and I pressed return, backspace, after statement2, it would put the cursor here:

if condition: 
  statement1
  statement2
<-- cursor position

If you have many idented levels and it doesn't do this, editing Python becomes impossible, since you have to backspace manually until you get to the right indentation level... it's error prone and annoying, e.g. if you had:

for something:
  for other:
    if hello:
      while x:
         statement1
         <-- How to indent back to level of "for other"?

EDIT: I was unable to get this to work while running emacs as "emacs -nw" (I am logging in to a server remotely and don't want the X interface to start). When I remove "-nw" and use emacs, with the slower X interface remotely, it all works... any idea why this might be? Could it be a shell configuration issue related to backspaces or something like that?

How can this be fixed? I just want it to backspace at indent levels if I'm inside a clause. thanks.


Source: (StackOverflow)