Coder Social home page Coder Social logo

python.el's Introduction

Python.el

python.el is now distributed with Emacs; this repository is no longer maintained.

Info

Install

If you are using Emacs 24.3 you already have a version of python.el. If you are using an Emacs 24.x prior 24.3 or you want to update your copy with some of the enhancements happening on Emacs add the following snippet in your .emacs:

(add-to-list 'load-path user-emacs-directory)

(defun my:ensure-python.el (&optional branch overwrite)
  "Install python.el from BRANCH.
After the first install happens the file is not overwritten again
unless the optional argument OVERWRITE is non-nil.  When called
interactively python.el will always be overwritten with the
latest version."
  (interactive
   (list
    (completing-read "Install python.el from branch: "
                     (list "master" "emacs-24")
                     nil t)
    t))
  (let* ((branch (or branch "master"))
         (url (format
               (concat "http://git.savannah.gnu.org/cgit/emacs.git/plain"
                       "/lisp/progmodes/python.el?h=%s") branch))
         (destination (expand-file-name "python.el" user-emacs-directory))
         (write (or (not (file-exists-p destination)) overwrite)))
    (when write
      (with-current-buffer
          (url-retrieve-synchronously url)
        (delete-region (point-min) (1+ url-http-end-of-headers))
        (write-file destination))
      (byte-compile-file destination t)
      destination)))

(my:ensure-python.el)

The master branch does its best to remain compatible with Emacs 24.x but it may break, although fixing compatibility breakage is priority (fill a bug report if you happen to find this). The "emacs-24" tends to be safer but new features are not added into it.

Whatever flavor you choose, if you are using a version prior to Emacs 24.3 you need cl-lib[0] which is available from GNU ELPA[1] package archive.

[0] http://elpa.gnu.org/packages/cl-lib.html [1] http://elpa.gnu.org/

Alternate method with el-get

If you are using el-get already you can install python24 for the current emacs-24 branch version or python for the master branch.

Introduction

This is now the official Python major mode for GNU Emacs.

It aims to provide the stuff you'll expect from a major mode for python editing while keeping it simple.

Currently it implements Syntax highlighting, Indentation, Movement, Shell interaction, Shell completion, Shell virtualenv support, Pdb tracking, Symbol completion, Skeletons, FFAP, Code Check, Eldoc, Imenu.

  • Syntax highlighting
  • Solid (auto)indentation support
  • auto-detection of indentation levels for current file
  • Robust triple quoted strings support
  • Fancy variable assignment colorization
  • Movement commands you'll expect from a major-mode.
  • Sexp-like movement
  • Python shell integration (not only for Python 2 but also Python 3!)
  • Python shell completion (Same as above!)
  • Python shell virtualenv support (as simple as setting a variable!)
  • PDB Tracking (it even supports ipdb!)
  • Symbol completion that sucks because a running inferior shell process and valid code in the current buffer are needed (Don't blame me, it's like that in every python-mode I know). Notice I don't recommend this thing, use ropemacs instead
  • Skeletons with a tight integration with dabbrev out of the box
  • FFAP (Find Filename At Point), click on an import statement and go to the module definition.
  • Code check via pychecker by default (this is customizable of course)
  • Eldoc support (this suffers the same drawbacks as the symbol completion, but it's the only sane way to do it from Elisp)
  • imenu support to easily navigate your code
  • add-log-current-defun support
  • hideshow support
  • outline support
  • fill paragraph (with customizable docstring formatting)

The code is well organized in parts with some clean sensitive naming.

Requirements

None, besides Emacs>=24.

Emacs 23?

Latest know version to work with Emacs 23 can be found at https://github.com/fgallina/python.el/tree/emacs23 any functionality/bugfixing back-port that may (or may not) happen in the future will be placed there.

Bug Reports

The github bug-tracker is being deprecated.

To report bugs, or to contribute fixes and improvements, use the built-in Emacs bug reporter (M-x report-emacs-bug) or send email to [email protected]. You can browse Emacs bug database at debbugs.gnu.org. For more information on contributing, see the CONTRIBUTE file (distributed with Emacs).

License

python.el is free software under the GPL v3, see LICENSE file for details.

python.el's People

Contributors

dandavison avatar eggert avatar fgallina avatar matthewlmcclure avatar monnier avatar rgmorris avatar tkf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python.el's Issues

Enable special highlighting for numbers.

It would be nice to have something like this (which I put in my init.el) so that python-mode has special highlighting of numbers. It's something I like(d) in VIM.

(make-face 'font-lock-number-face)
(set-face-attribute 'font-lock-number-face nil :inherit font-lock-constant-face)
(setq font-lock-number-face 'font-lock-number-face)
(defvar font-lock-number "[0-9]+\\([eE][+-]?[0-9]*\\)?")
(defvar font-lock-hexnumber "0[xX][0-9a-fA-F]+")
(defun add-font-lock-numbers ()
        (font-lock-add-keywords nil (list
                      (list (concat "\\<\\(" font-lock-number "\\)\\>" )
                       0 font-lock-number-face)
                      (list (concat "\\<\\(" font-lock-hexnumber "\\)\\>" )
                       0 font-lock-number-face)
                      )))

(add-hook 'python-mode-hook 'add-font-lock-numbers)

autoindent?

I'm wondering if it's possible to support "autoindent" in the same way that python-mode does? For example, if you have the following code:

def foo(self):

And the point is just at the right of the colon, pressing return in python-mode puts the cursor on the next line right beneath the "f" in foo. In python.el, it puts it all the way at the beginning of the line, and you need to hit the tab key to move it.

python-indent-calculate-indentation calculates indentation wrong

For following code the python-indent-calculate-indentation returns wrong offset:

if blah:
    do_something
else:
    if blah:
         do_something
    else: # For this, the function return 0 as offset, when it should be 4

The electric colon causes that the last else: drops all the way to the zero indentation level, as current-indentation is 4 and python-indent-calculate-indentation is 0.

python-shell-send-buffer widens narrowed region before sending buffer

Before sending the buffer to the shell, python-shell-send-buffer calls (widen) to disable any narrowing. I am not sure this is very useful. I expected it to honor the narrowing and send only the (narrowed) content of the buffer. Since narrowing has to be enabled explicitly by the user, I don't think this would lead to any confusion.

el-get installation

While installing using el-get I get this error "Error: IO error reading /Users/nyaruka/.emacs.d/el-get/python.el: Is a directory" this error is linked to the name of your repository.

It will be better if you just name it something different to the file itself.

Eldoc and long docstrings

Hi,
I don't know whether this is a python.el issue or whether it is an eldoc issue, but with very long docstrings, like quad of scipy (approx 70 lines), the bottom is shown rather than the top. Most likely, the user wants a quick view of possible arguments, not a snip from an example, which is usually found in the bottom of long docstrings.
This might be due to "bad" coding of docstrings, but long docstrings seems to be norm at least in scipy-related packages.

Maybe it would be possible to parse the docstring something similar to head?

Thanks again,
Rasmus
T

pdbtrack does not indicate correct line on Windows

When starting a debug session on Emacs 24.0.50 on Windows, pdbtrack does not mark the correct line in the source.

This happens because the following code (from line 1295):
(line-num
(save-excursion
(string-match
(format python-pdbtrack-stacktrace-info-regexp
(regexp-quote
inferior-python-mode-current-temp-file))
full-output)

inferior-python-mode-current-temp-file is inserted into the python-pdbtrack-stacktrace-info-regexp. On Windows the regexp quoted string looks like this "C:/Docu1/Temp/pyakdah" (or similar). However, the full-output of the shell on Windows contains the Windows style path, e.g. "C:\Docu1\Temp\pyakdah". Therefore, the string-match returns nil and the line-number is always 0.

One fix would be to do a string replace of '/' with '\'. However, I am not sure why the path to the file is even included in the python-pdbtrack-stacktrace-info-regexp. Maybe there is a cleaner more portable solution.

python-shell-send-buffer fails on simple file

In commit 6e46a01 sending simple files to the shell seems to be broken.

print "Hello World!"

works fine.

def foo():
    print "Hello World!"

fails, as does

class Foo():
    def __init__(self):
        print "Hello World!"

The error is always something like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:/devel/python/test.py", line 1
    def foo():
              ^
  SyntaxError: invalid syntax
  >>> 

pointing to the end of the first line.

Indentation issue in multi line "def"

def foo(word1, word2, word3, word4, word5,

When you type a long def like this, and need more arguments, you expect enter after "word5" to put your point under the "w" in "word1" so you can continue adding args, like so:

def foo(word1, word2, word3, word4, word5,
        word6, word7)

However enter puts my point at the start of the line, and will not move it.

(beginning|end)-of-defun functions do not work

In commit fdc4e56 the (beginning|end)-of-defun functions do not work.

The errors I get are:

  • Wrong type argument: stringp, nil
  • Lisp nesting exceeds `max-lisp-eval-depth'

Which error depends on where in the file the point is and whether I run C-M-a or C-M-e.

pdbtrack does not indicate correct line when using python-shell-send-file

When using python-shell-send-file with a file that contains pdb.set_trace() the pdbtrack indicator is not indicating the correct line. This is because the python-pdbtrack-comint-output-filter-function uses the name of the temporary file (executing the open/execute/close code) instead of the real .py file name whereas the full-output from the shell contains the name of the .py file.

ELPA conformance

You should add Version and URL fields in the file header comments to make it compatible with current ELPA standards. This way users will be able to submit to package repositories like Google's Marmalade.

Have a look here for more details.

Unexpected behavior of fill-paragraph in docstrings

When invoking M-q in a docstring, I see unexpected behavior. Given the following docstring:

"""A special-case of build_model that knows how to build a model based on
words from a corpus given as a string or a file-like object."""

Pressing M-q causes the docstring to be reformatted like so:

"""A special-case of build_model that knows how to build a model based on
words from a corpus given as a string or a file-like object.

"""

As you can see, an extra newline is inserted at the end. This is not how I prefer my docstrings to be formatted. The following code, in python-fill-paragraph-function at line 1511, controls this behavior:

;; If there is a newline in the docstring lets put triple
;; quote in it's own line to follow pep 8
(when (save-excursion
        (re-search-backward "\n" string-start-marker t))
  (newline)
  (newline-and-indent))

When I remove that code, my docstrings are filled as I'd like them to be.

Is there any chance we could have a setting to control this? I'd never encountered that particular bit of PEP 8 style. I don't think I can stand to have all of my multiline docstrings end in a blank line and then the closing quotes.

Thanks for the promising new Python mode!

Eldoc issue

  • Problem
    Eldoc-mode is great and extremely useful (i.e. the mode where a little
    man-snip is shown at the bottom of the buffer). However, several times
    enabling eldoc have made my Emacs freeze beyond rescue using python.el.

I use emacs -q (i.e. no unorthodox config) for test and python2 -i
as the inferior shell (python is python 3 in my distribution).

Here is a snip that /always/ causes a freeze. Move the cursor over
dif.append or the following line with eldoc-mode enabled in the
following.

dif = [0.]
    for i in range (1,1001) :
    dif.append(abs(
    e**(a*td[i])-u[i]
    ))

I don't know why it happens, though, but my best guess is a lost
connection to the inferior shell or lack of info from the inferior
shell... But I probably shouldn't be speculating.

  • Emacs Info
    In GNU Emacs 24.0.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.22.1)
    of 2011-03-12 on Pank
    Windowing system distributor The X.Org Foundation', version 11.0.10904000 configured usingconfigure 'CFLAGS=-march=native -O2 -pipe -fno-optimize-sibling-calls' '--prefix=/usr' '--sysconfdir=/etc' '--localstatedir=/var' '--libexecdir=/usr/lib' '--mandir=/usr/share/man' '--without-sound' '--with-x-toolkit=gtk' 'LDFLAGS=-Wl,--hash-style=gnu -Wl,--as-needed''

    Important settings:
    value of $LC_ALL: nil
    value of $LC_COLLATE: nil
    value of $LC_CTYPE: nil
    value of $LC_MESSAGES: nil
    value of $LC_MONETARY: nil
    value of $LC_NUMERIC: nil
    value of $LC_TIME: nil
    value of $LANG: en_DK.UTF-8
    value of $XMODIFIERS: nil
    locale-coding-system: utf-8-unix
    default enable-multibyte-characters: t

    Major mode: Python

    Minor modes in effect:
    eldoc-mode: t
    tooltip-mode: t
    mouse-wheel-mode: t
    tool-bar-mode: t
    menu-bar-mode: t
    file-name-shadow-mode: t
    global-font-lock-mode: t
    font-lock-mode: t
    blink-cursor-mode: t
    auto-composition-mode: t
    auto-encryption-mode: t
    auto-compression-mode: t
    line-number-mode: t
    transient-mark-mode: t

Indentation issue with electric-pair-mode under Emacs24

Hi all

I've been playing around with emacs-prelude, an Emacs 24 only package which uses python.el as the default python mode.

I've come across an issue with indentation which is probably easiest understood by looking at the 43 second screencast at - http://screencast.com/t/IQGNUV6T2qWW - some discussion can be found at https://github.com/bbatsov/emacs-prelude/issues/25

On the screen cast the left hand side is emacs-prelude with python.el and the right hand side the stock emacs24 python-mode.el (it is emacs 24 running under emacs -Q). The inserted closing parenthesis from electric-pair-mode seems to confuse python.el. I assume if you do this manually under Emacs23 the same issue would occur.

Any suggestions welcomed. If there is anything I can do to further test please let me know.

Many thanks

Ian

Drop indentation level by one when writing "else:" or "elif:"

When writing an if sentence, the intendation is not automatically reduced by one when writing an "else:". It would be cool if inserting the colon after "else" would make the indentation to decrease by one. Existing python-mode already does this, so it probably could be nicked from there ;)

Example of current behaviour:

if:
    # this correctly adds intendation by one
    do_something()
    else:
          # Writing the colon after else should take intendation back one step
          do_something_else()

Completion in python 3

Hi,
Your python.el seems to provide the best support for py3. However, I have not been able to make completion work correctly using ipython3.
I use the following
(setq
python-shell-interpreter "ipython3"
python-shell-interpreter-args "--classic --colors linux"
python-shell-prompt-regexp "In [[0-9]+]: "
python-shell-prompt-output-regexp "Out[[0-9]+]: "
python-shell-completion-setup-code ""
python-shell-completion-string-code
"';'.join(__IP.complete('''%s'''))\n")

The problem probably lies in python-shell-completion-string-code. The weirdest thing is that completion does not work in the inferior python shell, but it works when ipython3 is started from an ordinary shell outside of Emacs. I am just starting on Python for a Computational Mathematics course. . .

Thanks,
Rasmus

getting python shell rather than ipython

I have looked through previous (now closed) issues on the subject, but I'm encountering a problem getting ipython to run. Rather than either 'run-python' or C-c C-c opening ipython, python is opened instead.

I have python.el installed from the marmalade repos on Emacs 24.

Here is the relevant snippet from my init.el

(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args ""
python-shell-prompt-regexp "In \[[0-9]+\]: "
python-shell-prompt-output-regexp "Out\[[0-9]+\]: "
python-shell-completion-setup-code ""
python-shell-completion-string-code "';'.join(__IP.complete('''%s'''))\n")

Feature Request: Eval Line

Hi,
ESS has and extremely useful function called Eval Line (C-c C-j). I miss it in python.el.

Personally, I think it would be nice and sufficient if python.el Eval Region (C-c C-r) would default to the current line if no region is selected.

Thanks,
Rasmus

region cleared out after indent

In python-mode.el, after indenting a region, the region is not cleared so you can immediately indent the block again. This comes in quite handy. It would be nice if this were available.

Indention oddity

if True:
    if True:
        pass
    else:
        pass

Place your point after the ":" in the else and press tab, it will de-indent but not cycle through indent options with further tabs. However, you start your point at the beginning of "else" (or before it) it will cycle between the two possible indents.

Is this expected? It's certainly odd when I run up against it.

python-shell-send-file is pollutes buffer list

The following line in python-shell-send-file (l. 1216)

(find-file-noselect file-name)

will open the temporary buffer. However, the buffer stays open and since python-shell-send-file is called by a lot of shell operations, after a while I have dozens of temporary files open in my buffer list.

I am not sure what this line is doing anyway. It almost looks like debug code?

Christoph

problems with indentation

whenever I try to indent something I get

let*: Arithmetic error

Tell me if you need that I run something to get more info.

system: Archlinux, emacs 23.2.1

Indentation problem with escaped newlines

Usually (in python-mode.el, Eclipse, ...) a new indentation level will be opened when a statement is splited in multiple lines e.g.

def foo():
    long_var1, \
    long_var2 = 1, 2

Should look like:

def foo():
    long_var1, \
        long_var2 = 1, 2

New IPython completion

Hi,
IPython2 has a new completion mechanism (and a new interesting QT console).

Thus,
;; python-shell-completion-string-code
;; "';'.join(__IP.complete('''%s'''))\n")

doesn't work.

I have tried

"';'.join(get_ipython().Completer.all_completions('''%s'''))\n"

"';'.join(get_ipython().Completer.complete('''s''')[1])\n"

but neither seems to function quite well

--Rasmus

Minor font-lock fixes

at least import and from should be mapped to font-lock-preprocessor-face.

(font-lock-add-keywords 'python-mode `((,(rx symbol-start (or "import" "from") symbol-end) 0 font-lock-preprocessor-face)))

and @property and other decorators should map to,

 (font-lock-add-keywords
  'python-mode
  `(("^[    ]*\\(@\\)\\([a-zA-Z_][a-zA-Z_0-9.]+\\)\\((.+)\\)?"
     (1 'font-lock-preprocessor-face)
     (2 'font-lock-builtin-face))))

since @ is a preprocessor command and whatever follows should probably just follow the builtin syntax, even though what follows can be defined by the user also.

I am trying to get the solarized theme to match with the VIM version and I am just finding these things which are minor changes which can make things a little more clear and colorful for us so inclined.

I am very busy atm but in a month i'll have time and hopefully i can submit a more formal patch if this isn't easy already. I don't know about font-lock in modes, just learned enough to get these minor patches in.

Indentation problem with function arguments

In Pep 8 is a hint that function arguments should be distinguished from the function body, e.g.

def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

Should look like:

def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

Electric colon dedents wrong

Directly from an email from Brett Hoerner

def func():
    for x in open('file'):
        try:
            pass
    except Exception: # after I've added the :, note that the except
    # was just de-indented and now lines up with my "for" and not the
    # "try" that it should line up with

The same code for the "Closes " will be useful to check this :)

python.el doesn't compile in Emacs 24

I'm using Emacs 24.0.50.1 (from git, I believe, built on 26 th January 2011), and tried to compile python.el both using my ordinary Emacs setup (didn't work) and a plain Emacs (using emacs -Q -nw), and they both provided me with the following error message when I tried to compile it:

Compiling file /home/oscar/git/python.el/python.el at Fri Feb 18 09:31:15 2011
python.el:275:16:Error: Symbol's value as variable is void: python-rx-constituents

Something weird with python.el and pymacs

If I startup and edit a python file, autocompletion using ropemacs works fine.

If I then open a python-shell (C-c C-z) running python -i, then when I try again to use autocompletion in ropemacs, my emacs freezes.

I am using:

GNU Emacs 24.0.50.1 (x86_64-apple-darwin10.6.0, NS apple-appkit-1038.35) of 2011-03-15

I will do some more testing and maybe I need to use Pymacs 0.24.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.