You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Function expected an indented block - question #907
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:
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.
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)
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?
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.
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).
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.
@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.
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 :-)
@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! :-)
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
Activity
jorgenschaefer commentedon Jun 7, 2016
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 benilin the Python interaction buffer)bmpalatiello commentedon Jun 7, 2016
I placed
(setq-default indent-tabs-mode nil)into my.emacsfile alone and that did not work. I apologize, but how would I set that in the Python interaction buffer?jorgenschaefer commentedon Jun 7, 2016
What value does
C-h v indent-tabs-modein both the Python buffer and the Python shell buffer report?bmpalatiello commentedon Jun 7, 2016
In the python shell buffer I get:
and in the Python buffer I get the same except underneath
Original value was tI getLocal in buffer brett.py; global value is the same.jorgenschaefer commentedon Jun 7, 2016
Hrm. I am stupid, sorry. You are using
C-enterto send individual lines. That does not work with function definitions I'm afraid – just useC-M-xto send the current function (or rather, top-level block).bmpalatiello commentedon Jun 7, 2016
Excellent! I'm glad it was a simple issue on my end. Thank you!
jorgenschaefer commentedon Jun 7, 2016
Sorry I was a bit slow there. Glad things worked out, and enjoy Elpy! :-)
mabbs90 commentedon Sep 2, 2016
Hi, I have the same issue even with loops. Like in the following:
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:
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:
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.
gopar commentedon Sep 2, 2016
@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.mabbs90 commentedon Sep 2, 2016
@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.
jorgenschaefer commentedon Sep 3, 2016
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!
mabbs90 commentedon Sep 3, 2016
@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! :-)
mashu commentedon Oct 26, 2016
Is there any walk-around or short hack that could fix sending individual lines with C-Enter ?
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
jorgenschaefer commentedon Oct 28, 2016
No.