Shortcut key for commenting out lines of Python code in Spyder

PythonSpyder

Python Problem Overview


I recently changed from the Enthought Canopy Python distribution to Anaconda, which includes the Spyder IDE.

In Canopy's code editor, it was possible to comment and uncomment lines of code by pressing the "Cntrl+/" shortcut key sequence. In Spyder I was unable to find an equivalent shortcut key in the introductory tutorial.

Is there a shortcut key for commenting and uncommenting code in Spyder?

Python Solutions


Solution 1 - Python

  • Single line comment

    Ctrl + 1

  • Multi-line comment select the lines to be commented

    Ctrl + 4

  • Unblock Multi-line comment

    Ctrl + 5

Solution 2 - Python

On macOS:

Cmd + 1

On Windows, probably

Ctrl + (/) near right shift key

Solution 3 - Python

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

Solution 4 - Python

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.

Solution 5 - Python

  • 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.

Solution 6 - Python

on Windows F9 to run single line

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKurt PeekView Question on Stackoverflow
Solution 1 - PythonSuresh2692View Answer on Stackoverflow
Solution 2 - PythonReblochon MasqueView Answer on Stackoverflow
Solution 3 - PythonTiago Martins PeresView Answer on Stackoverflow
Solution 4 - PythonArpit SharmaView Answer on Stackoverflow
Solution 5 - Pythongoat1123View Answer on Stackoverflow
Solution 6 - PythonPydView Answer on Stackoverflow