Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

How to get vs code to auto insert if __name_ ... snippet?

How to configure or enable visual studio code to automatically insert the standard:

if __name__ == '__main__':

I see it was implemented in 2018 but the usage being discussed in that ticket does not trigger anything for me. I've been scanning through the docs and general internet but my search keywords are not turning up relevant pages.

question from:https://stackoverflow.com/questions/66050221/how-to-get-vs-code-to-auto-insert-if-name-snippet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've noticed the same thing -- I wonder if this snippet was removed, or if its presence depends on the language server used (I believe VSCode switched from MS's language server to Pylance by default).

In any case, you can recreate the extension yourself by clicking the menu item Code > Preferences > User Snippets, select python.json, and then enter the following entry:

"Main": {
    "prefix": "__main__",
    "body": [
        "if __name__ == "__main__":",
        "${1:pass}"
    ],
    "description": "Insert main block"
}

More info: https://code.visualstudio.com/docs/editor/userdefinedsnippets


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...