Add a snippet for the `if __name__ == '__main__':` block
See original GitHub issueWe 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:
- Open a new editor window
- Create a new file
helloworld.py
- 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|
- 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:
- Created 5 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I personally just want:
Or whatever is required to have VS Code start the cursor indented in the
if
block.Instead of
pass
that’s where the cursor should be placed.