Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap __name__ to something other than __main__ in Python REPLs #540

Closed
Olical opened this issue Nov 23, 2023 · 3 comments
Closed

Swap __name__ to something other than __main__ in Python REPLs #540

Olical opened this issue Nov 23, 2023 · 3 comments
Labels
client-python enhancement New feature or request

Comments

@Olical
Copy link
Owner

Olical commented Nov 23, 2023

As a few people have been discussing on Discord, the Python REPL leaves __name__ bound to the default __main__ which means your __main__ block is run when you load a buffer into the REPL. This is pretty unintuitive and probably dangerous.

We should bind __name__ to __repl__ or maybe even the name of the Python module you're currently editing if that can always be cleanly inferred. Looking at what other tooling does here may also be helpful especially if people already have expectations about how this should work from other tools.

@Olical Olical added enhancement New feature or request client-python labels Nov 23, 2023
@russtoku
Copy link
Contributor

Would deleting the __name__ variable be a reasonable thing to do so that the REPL environment does think that it's running a program or script when it starts up?

$ python
Python 3.11.6 | packaged by conda-forge | (main, Oct  3 2023, 10:40:37) [Clang 15.0.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __name__
'__main__'
>>> del __name__
>>> __name__
'builtins'
>>> print(__name__)
builtins
>>> del __name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__name__' is not defined
>>> __name__ == '__main__'
False

In part 6, Modules, in the Python Tutorial it says:

To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

@Olical
Copy link
Owner Author

Olical commented Dec 3, 2023

Rather than deleting (just in case someone is assuming that it'll exist and be a string) I've set __name__ = '__repl__' on setup. Hopefully this should fix things and be intuitive. This is on the develop branch, please let me know if this works in your use cases!

@russtoku
Copy link
Contributor

russtoku commented Dec 3, 2023

That should provide less surprise. Thanks!

@Olical Olical closed this as completed Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client-python enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants