Wolf icon indicating copy to clipboard operation
Wolf copied to clipboard

__name__=="__main__" is False

Open JoaquinKeller opened this issue 5 years ago • 3 comments

if __name__=="__main__":
     print(__name__)
     # this should run, no ?

JoaquinKeller avatar Jan 16 '20 15:01 JoaquinKeller

@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 :)

Duroktar avatar Jan 17 '20 11:01 Duroktar

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.

Jorgee97 avatar Jun 24 '20 00:06 Jorgee97

@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.

Almenon avatar Jun 24 '20 01:06 Almenon