samuelcolvin / python-devtools

Dev tools for python
https://python-devtools.helpmanual.io/
MIT License
985 stars 47 forks source link

Instructions for using without import fail on Python 3.10 #102

Closed ssteinerx closed 2 years ago

ssteinerx commented 2 years ago

https://python-devtools.helpmanual.io/usage/#usage-without-import

Suggests the following snippet:

    __builtins__['debug'] = debug

In python 3.10 (at least, only version tried), referencing builtins directly gives "TypeError: 'module' object is not subscriptable"

builtins.dict, however, is subscriptable.

That figuring out where sitecustomize.py (or usercustomize.py) actually can be put so it gets properly executed on startup on a multi-python and/or brew (linuxbrew) system is an absolute nightmare is a different issue and not one I'd expect python-devtools to address particularly.

samuelcolvin commented 2 years ago

Interesting, I'll look back at what I have.

That figuring out where sitecustomize.py (or usercustomize.py) actually can be put so it gets properly executed on startup on a multi-python and/or brew (linuxbrew) system is an absolute nightmare is a different issue and not one I'd expect python-devtools to address particularly.

Agreed, printing sys.path can help a lot, we should add this to the docs too.

alexmojaki commented 2 years ago

https://stackoverflow.com/questions/11181519/whats-the-difference-between-builtin-and-builtins

__builtins__ can be either the module or the dict in the same python version, depending on context.

samuelcolvin commented 2 years ago

Maybe we could add devtools.install() to take care of this?

alexmojaki commented 2 years ago

That would fit well with https://github.com/gruns/icecream#import-tricks and https://github.com/alexmojaki/snoop#install

But you can also just change the recommendation to import builtins; builtins.debug = debug and that should work consistently, since this is Python 3 only.

samuelcolvin commented 2 years ago

See #108, I've added a tiny install CLI.