question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a snippet for the `if __name__ == '__main__':` block

See original GitHub issue

We have all sorts of useful snippets in our extension, but I’ve noticed that we have not added one for the ubiquitous block:

if __name__ == '__main__':

Proposal to add the snippet with the type-ahead identifier __main__.

Acceptance Criteria:

  1. Open a new editor window
  2. Create a new file helloworld.py
  3. Add the following code:
1|  import sys
2|  
3|  def hello_world():
4|      print('Hi there!')
5|      print(f'Python version {sys.version_info}')
6|  
7|
  1. On line 7, begin type __main

Expected Result

Popup with a snippet for the dunder name equals dunder main appears. Pressing Enter will accept the snippet and insert code such as:

6|
7|  if __name__ == '__main__':
8|      $
9|

…where the $ is where the cursor is left such that the user can add whatever call(s) are necessary.

EDIT: Acceptance criteria altered to ensure cursor is placed optimally.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
brettcannoncommented, Aug 1, 2018

I personally just want:

if __name__ == "__main__":
    ${1}

Or whatever is required to have VS Code start the cursor indented in the if block.

3reactions
DonJayamannecommented, Jul 24, 2018

Instead of pass that’s where the cursor should be placed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get vs code to auto insert if __name__ ... snippet?
In vscode, File -> Preferences -> User Snippets. Type python and choose python. A json file will open · Copy-paste all or the...
Read more >
Python if __name__ == __main__ ...
When a Python interpreter reads a Python file, it first sets a few special variables. Then it executes the code from the file....
Read more >
What Does if __name__ == "__main__" Do in Python?
In Short: It Allows You to Execute Code When the File Runs as a Script, but Not When It's Imported as a Module....
Read more >
What does the if __name__ == “__main__”: do?
Here, as we executed script.py directly __name__ variable will be __main__. So, code in this if block[Block 2] will only run if that...
Read more >
Understanding if __name__ == “__main__” in Python - Medium
However, if a module is being run directly (from command line), then __name__ instead is set to the string “__main__”. Let's put together...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found