Skip to content

Function expected an indented block - question #907

Closed

Description

@bmpalatiello

I made the switch to Emacs. I am using Elpy within Emacs as an IDE. My setup is side-by-side windows, on the left is a buffer(script) where I write/edit the code which then gets sent to the IPython shell on the right with Ctrl-Enter. When I type the function:

import numpy as np
import pandas as pd

data = pd.read_csv('spx_index.csv')

def convert(x):
    return x.ix[:, 1:].set_index(x.ix[:, 0])

into the script (4-space indentation) and press Ctrl-Enter twice I get:

>>> ...   File "<stdin>", line 2
       return x.ix[:, 1:].set_index(x.ix[:, 0]) 
               ^
IndentationError: expected an indented block

However, when I copy the function and paste it directly into the IPython shell:

>>> def convert(x):
       return x.ix[:, 1:].set_index(x.ix[:, 0]) 
... ... 
>>> 

It works and the function is saved.

Getting the function to run directly from the script to the shell would be ideal. I can't imagine having to copy and paste every function into the shell.

Activity

  1. added this to the v1.13 milestone on Jun 7, 2016
  2. jorgenschaefer commented on Jun 7, 2016

    @jorgenschaefer
    Owner

    Hello, and thanks for the report! I think this is a problem in how Emacs handles tabs vs. spaces in various buffers – and might very well be related to #890. Could you try (setq-default indent-tabs-mode nil) and see if the problem persists? (That variable should be nil in the Python interaction buffer)

  3. bmpalatiello commented on Jun 7, 2016

    @bmpalatiello
    Author

    I placed (setq-default indent-tabs-mode nil) into my .emacs file alone and that did not work. I apologize, but how would I set that in the Python interaction buffer?

  4. jorgenschaefer commented on Jun 7, 2016

    @jorgenschaefer
    Owner

    What value does C-h v indent-tabs-mode in both the Python buffer and the Python shell buffer report?

  5. bmpalatiello commented on Jun 7, 2016

    @bmpalatiello
    Author

    In the python shell buffer I get:

    indent-tabs-mode is a variable defined in `C source code'.
    Its value is nil
    Original value was t
    
    Automatically becomes buffer-local when set.
    This variable is safe as a file local variable if its value
    satisfies the predicate `booleanp'.
    
    Documentation:
    Indentation can insert tabs if this is non-nil.
    
    You can customize this variable.
    
    [back]
    

    and in the Python buffer I get the same except underneath Original value was t I get Local in buffer brett.py; global value is the same.

  6. jorgenschaefer commented on Jun 7, 2016

    @jorgenschaefer
    Owner

    Hrm. I am stupid, sorry. You are using C-enter to send individual lines. That does not work with function definitions I'm afraid – just use C-M-x to send the current function (or rather, top-level block).

  7. bmpalatiello commented on Jun 7, 2016

    @bmpalatiello
    Author

    Excellent! I'm glad it was a simple issue on my end. Thank you!

  8. jorgenschaefer commented on Jun 7, 2016

    @jorgenschaefer
    Owner

    Sorry I was a bit slow there. Glad things worked out, and enjoy Elpy! :-)

  9. mabbs90 commented on Sep 2, 2016

    @mabbs90

    Hi, I have the same issue even with loops. Like in the following:

    for i in range(20):
        print(i)
    

    pressing C-Enter on the first line sends the first line to the python shell, and pressing C-Enter on the second line, raises the following error:

    >>> ...   File "<stdin>", line 2
        print(i)
            ^
    IndentationError: expected an indented block
    

    Is there any way we can add something to the .emacs file to fix this behavior?

    I have also tried C-M-x but the result is not desired:

    >>> 19
    

    This is what it returns, even though it is supposed to print integers 0, 1, ..., 19

    I think it will be much comfier to use C-Enter for any line regardless of it being a function/loop/etc. as long as the spacing is observed, but I don't know what complexity it would bring to elpy.

  10. gopar commented on Sep 2, 2016

    @gopar
    Collaborator

    @mabbs90 If you mark the region with and call elpy-shell-send-region-or-buffer (C-c C-c), I think that will do what you want.

  11. mabbs90 commented on Sep 2, 2016

    @mabbs90

    @gopar Yeah that's a good workaround. Thanks! but I hope to see C-Enter also working some time. I use emacs primarily for R and since R doesn't care about the indentation, C-Enter always works seamlessly. You can hold Ctrl and press enter and go all the way down a giant function line by line among your code. Hopefully we can have the same thing in elpy if others also see the necessity.

  12. jorgenschaefer commented on Sep 3, 2016

    @jorgenschaefer
    Owner

    Python is not R, I'm afraid. Not everything that works easily in R works easily 1:1 in Python. I don't think there's a single clear thing that Emacs should do in those situations that would not require reading the user's mind, so I won't be working on this feature I'm afraid. If you have a good idea, feel free to implement it – but beware that it needs to behave in a way that makes sense to most people for me to consider it :-)

    I hope you enjoy Elpy despite of this, though!

  13. mabbs90 commented on Sep 3, 2016

    @mabbs90

    @jorgenschaefer That's totally understandable and I agree! What made me transition to elpy from ipython notebook is I guess the ability to send and evaluate the codes line by line; and of course being an emacs user for quite some time. And I agree that if there's no consensus on this request, you should not be working on it :) But I still think it's a good capability and adding it to elpy will not only be a complement to the current line-to-console feature; but also it will make it standout even more when it comes to using an IDE for python among all the choices people have.
    Regardless, I do enjoy elpy and I greatly appreciate you all for all your good work! :-)

  14. mashu commented on Oct 26, 2016

    @mashu

    Is there any walk-around or short hack that could fix sending individual lines with C-Enter ?

    IndentationError: expected an indented block
    >>> 
    

    Suggested alternative C-M-x does not work for me, it seems to always be sending top level function, which is not what I want most of times (or possibly a bug as it should I belive send block ?). I would really like line by line behaviour.

    If line ends with ":" I think it should be expecting new line to come. I would be grateful for quick hack/fix that makes C-Enter working, does not need to be part of elpy but would be nice if solution could be found amongst reported issues for people that want this particular behaviour

  15. jorgenschaefer commented on Oct 28, 2016

    @jorgenschaefer
    Owner

    Is there any walk-around or short hack that could fix sending individual lines with C-Enter ?

    No.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions