如何在VS代码中缩进jupyter单元/块

发布于 2025-02-06 20:14:24 字数 657 浏览 2 评论 0原文

我想将VSCODE和JUPYTER用于交互式Python编程。一切都可以找到,除了单元格/块不在缩进或未缩进代码时才识别。以前,我在可能的情况下使用了Spyder。

我喜欢这种行为,因为当您具有较长的类或功能定义(缩进)时,它对于调试UND开发很有用,但只想在类交互式上运行几行。

我尝试了正式的Python扩展名和Don Jayamanne Juypter扩展。没有人允许所需的行为。


更新:我只是发现不同的Python版本在这方面的行为有所不同。使用Python 3.6.6和3.7.2,凹痕引起了错误,而Python 3.6.7或3.6.8缩进凹痕被忽略并执行单元格。是否有解释,为什么不同版本的行为不同或明确的设置来处理此问题?

#%%
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')

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-02-13 20:14:24

Python使用Whitespace凹痕,而不是卷发括号或关键字来界定块。看起来VSC Jupyter扩展创作者似乎并没有解决此问题。

一个黑客是在单元开头添加true if语句,诸如此类:

#%%
if 1:  # indent for emphasis
    print('This works')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文