3

I want to use vscode and jupyter for interactive python programming. Everything works find except that cells/blocks are only recognized when they are not indent or if the code is not indented. Previously, I used spyder where this is possible.

I like this behavior because it is useful for debugging und development when you have longer class or function definitions (that are indented) but just want to run a couple of lines from the class/function interactively.

I have tried the official python extension and the Don Jayamanne juypter extension. None allowed the desired behavior.


Update: I just figured out that different python versions behave differently in this respect. With python 3.6.6 and 3.7.2 the indents raise an error while with python 3.6.7 or 3.6.8 the indent is ignored and the cell is executed. Is there an explanation why different versions behave differently or explicit setting to handle this?

#%%
print('This works')

#%% 
    print('This does not work, as it raises an indent error')
    print('Update: This works in python version 3.6.7 or 3.6.8')

    #%%
    print('This does not work, as it is not recognized as a cell/block')
hstorm
  • 31
  • 4

1 Answers1

0

Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks. It doesn't look like the VSC Jupyter Extension creators went out of their way to work around this.

One hack is to add a true if statement at the beginning of your cell, like this:

#%%
if 1:  # indent for emphasis
    print('This works')
Alex F
  • 756
  • 2
  • 9
  • 24
  • 1
    Thanks for suggestions. This works but it is not really solving my problem. I often have a function from which I one to run a part in an interactive mode for development or debugging. But without changing the code, i.e. such that the program can still be run as a whole. – hstorm Jan 18 '19 at 12:08