#๐Ÿ”’ Why did I get this message while trying to use dunder name == dunder 'main?' (I use VS Code)

17 messages ยท Page 1 of 1 (latest)

zealous canyon
#

This is the message that appears: Main is not defined
[{
"resource": "/C:/Users/ASUS ID/Python folders/Testing/.test.py",
"owner": "Pylance3",
"code": {
"value": "reportUndefinedVariable",
"target": {
"$mid": 1,
"path": "/microsoft/pylance-release/blob/main/docs/diagnostics/reportUndefinedVariable.md",
"scheme": "https",
"authority": "github.com"
}
},
"severity": 4,
"message": ""main" is not defined",
"source": "Pylance",
"startLineNumber": 2,
"startColumn": 5,
"endLineNumber": 2,
"endColumn": 9,
"modelVersionId": 19,
"origin": "extHost1"
}]

I'm new and trying to learn module packages, I used snippets for this
ctrl + shift + p > configure snippets > python.json and i typed this outside the curly brackets on a new line:
}
"Main": {
"prefix": "main",
"body": [
"if name == "main":",
"\t${1:pass}"
],
"description": "Insert main block"
}

what can i do to learn module and help this work?

little cosmosBOT
#

@zealous canyon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

winter niche
#

!code

little cosmosBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

winter niche
#

Things like __ are markdown, and outside a code block they vanish, changing the rendering of the text.

zealous canyon
#

Thanks for the suggestion, I'll try that out after going home

hidden badger
#

i'm assuming the body part looks something like

"body": [
    "if __name__ == "__main__":",
    "    ${1:pass}"
],

if so, that's probably not gonna work. either swap the quotes on main to 's or escape both "s

hidden badger
#

in your python.json snippets file

winter niche
#

You might also need to tell us a bit about your dev environment, since this looks like som python code buried in some JSON, making things harder.

zealous canyon
#

Do you have an easier alternative

#

@winter niche

winter niche
#

Can you first edit your opening message to put code blocks around the json?
```json
your JSON chunk
goes here
```
i.e. insert the ```json line above and the ``` below.

#

The normal python "main" boilerplate looks like this:

if __name__ == "__main__":
    ... run the main programme ...
little cosmosBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.