Duroktar / Wolf

Wolf is a VsCode extension that enables live inspection of Python code in the editor.
Other
128 stars 7 forks source link

__name__=="__main__" is False #47

Open JoaquinKeller opened 4 years ago

JoaquinKeller commented 4 years ago
if __name__=="__main__":
     print(__name__)
     # this should run, no ?
Duroktar commented 4 years ago

@JoaquinKeller see what print(__name__) #? outputs. That'll show you exactly what's being compared. In Wolf, executed code is loaded a little differently so it's likely the __name__ variable is set to something other than "__main__".

Let me know if this helps! I hope you're enjoying the extension otherwise :)

Jorgee97 commented 4 years ago

Hey @Duroktar Old issue, but the problem is still there, printing name doesn't return anything at all. Any idea of the why? Hit me up if you want me to provide more data.

Almenon commented 4 years ago

@Duroktar in arepl I set specialVars by specifying them in my python code, like so:

specialVars = ["__doc__", "__file__", "__loader__", "__name__", "__package__", "__spec__", "arepl_store"]
# later on...
# copy all special vars (we want execd code to have similar locals as actual code)
# not copying builtins cause exec adds it in
# also when specialVars is deepCopied later on deepcopy cant handle builtins anyways
for var in specialVars:
    starting_locals[var] = locals()[var]

# users code should execute under main scope
starting_locals["__name__"] = "__main__"
starting_locals["__loader__"].name = "__main__"

# spec should not exist when executing file directly
del starting_locals["__spec__"]

You can copy the same approach if you want.