在 Spyder 中注释掉 Python 代码行的快捷键

我最近将 Enthought Canopy Python 发行版改为 Anaconda,其中包括 Spyder IDE。

在 Canopy 的代码编辑器中,可以通过按下“ Cntrl +/”快捷键序列来注释和取消注释代码行。在 Spyder 中,我无法在入门教程中找到相应的快捷键。

在 Spyder 中有注释和取消注释代码的快捷键吗?

297296 次浏览

On macOS:

Cmd + 1

On Windows, probably

Ctrl + (/) near right shift key
  • Single line comment

    Ctrl + 1

  • Multi-line comment select the lines to be commented

    Ctrl + 4

  • Unblock Multi-line comment

    Ctrl + 5

on Windows F9 to run single line

Select the lines which you want to run on console and press F9 button for multi line

  • Unblock multi-line comment

    Ctrl+5

  • Multi-line comment

    Ctrl+4

NOTE: For my version of Spyder (3.1.4) if I highlighted the entire multi-line comment and used Ctrl+5 the block remained commented out. Only after highlighting a small portion of the multi-line comment did Ctrl+5 work.

Yes, there is a shortcut for commenting out lines in Python 3.6 (Spyder).

For Single Line Comment, you can use Ctrl+1. It will look like this #This is a sample piece of code

For multi-line comments, you can use Ctrl+4. It will look like this

#============= \#your piece of code \#some more code \#=============

Note : \ represents that the code is carried to another line.

While the other answers got it right when it comes to add comments, in my case only the following worked.

  • Multi-line comment

    select the lines to be commented + Ctrl + 4

  • Multi-line uncomment

    select the lines to be uncommented + Ctrl + 1