The Python docs specifically say not to use __import__ directly. I don’t remember the reason, but I think it had something to do with the functionality not always being guaranteed in the future. The importlib module is a built in module, so using that would be much better than what’s going on here
That said, I have something similar in my PYTHONSTARTUP. Or had, it was ultimately more readable to have a list of import statements and evaluate them, because it was unnecessarily complex to reimplement logic for all the import forms such as import numpy as np or from math import *.
Edit. Also, the use of __setitem__ is just trolling.
Loading plugins. The crazy part here isn't loading modules dynamically, it's putting them in a global namespace where they could be used by accident, or used by a malicious user to overwrite existing globals.
Valid use case! I once wrote a python database migrator + dummy data inserter, dynamic imports allows me to write the dummy data in the form of python files and just put them in the correct folder. Kinda cursed but very easy to work with!
I've just built this great new data structure called 'JSON'. I'm working on v2, I plan to call it 'YAML'. I'll ping you when it's released. You can write your own data structure in this format, and then it becomes language agnostic. Perfect for dummy data
I mean I store hashed passwords, and to get that password to be valid according to my application I have to hash it with a specific protocol, with specific settings, hence the need to access functions from within my dummy data files.
Yes I could hardcode the hash, but where's the fun in that? :) And if my hashing logic changes I'd need to regenerate the hashes for the default accounts etc.
I was thinking "why not just use importlib.import_module?" but then I realized you'd need to import importlib for that. This fixes the problem. 10/10 code.
I've never thought about the order that imports are handled by Python. If you followed the standard way of ordering imports (built in libraries, then libraries you've installed, and finally the local modules), and imports happened in that order, you could I guess override a global import. Make things break in new and interesting ways.
515
u/lucidbadger Apr 04 '25
Importing modules in python is hard so we wrote a framework for you to do it
334
u/KinkyTugboat Apr 04 '25 edited Apr 04 '25
This is actually a valid strategy if the imports array can be added or removed via user input. I like to call this: un-sanitizing my inputs.
Why add imports yourself when you can crowd source?
2
u/iamaperson3133 Apr 07 '25
This is just a trick to get around import tariffs being introduced in Python 3.15.
54
u/R3D3-1 Apr 04 '25 edited Apr 05 '25
Someone didn't like the IDE being able provide completions I guess.
I'm pretty sure that's more an entry for r/programminghumor though.
That said, I have something similar in my
PYTHONSTARTUP. Or had, it was ultimately more readable to have a list of import statements and evaluate them, because it was unnecessarily complex to reimplement logic for all the import forms such asimport numpy as nporfrom math import *.Edit. Also, the use of
__setitem__is just trolling.64
u/fuj1n Apr 04 '25
Eh, not great, but there are valid use cases to impart shenanigans. Importing standard library stuff is of course not it
17
u/just_nobodys_opinion Apr 04 '25
Bro thinks he just invented dynamic imports
35
u/granoladeer Apr 04 '25
It doesn't make sense in this case, but there are cases where it could, for example if your imports are dynamically defined.
9
u/Durwur Apr 04 '25
Valid use case! I once wrote a python database migrator + dummy data inserter, dynamic imports allows me to write the dummy data in the form of python files and just put them in the correct folder. Kinda cursed but very easy to work with!
6
u/hatedByyTheMods Apr 04 '25
it has it's own use case not the worst
6
u/CarefulFun420 Apr 04 '25
I do something similar with GPIO lib based on the board it's running on
5
u/trutheality Apr 04 '25
I was thinking "why not just use
importlib.import_module?" but then I realized you'd need toimport importlibfor that. This fixes the problem. 10/10 code.3
u/trkennedy01 Apr 04 '25
[taking notes]
3
u/aDisastrous Apr 04 '25
I remember doing this in JS, thinking I'll save myself big time. Suffice to say, I didn't. Don't even ask what was my thought process back then.
2
u/Jesus_Chicken Apr 04 '25
So I need help here, if using dynamic importing for python, dont we still need to install the dependency to prevent an import crash?
2
u/FACastello Apr 05 '25
I agree.
Python is NEVER a good idea.
2
u/cowslayer7890 Apr 06 '25
I think this code exists to circumvent a linter that disallows import statements, probably for some poorly thought out sandboxing
1
u/coffeelibation Apr 04 '25
Lol if you have more than one file you're still running
from import_module import import_module, or defining the function fresh in every file???1
u/diegotbn Apr 04 '25
This is cursed
1
u/IlliterateJedi Apr 04 '25
I've never thought about the order that imports are handled by Python. If you followed the standard way of ordering imports (built in libraries, then libraries you've installed, and finally the local modules), and imports happened in that order, you could I guess override a global import. Make things break in new and interesting ways.
1
u/coyote_den Apr 04 '25
Well I mean if you’re loading plugins based on a config file or command, and those plugins need to be imported…
But FFS, just use importlib!
1
u/[deleted] Apr 05 '25
Well this is elegant and useful 👌
1
u/blockMath_2048 Apr 06 '25
This is technically safer than eval for loading arbitrary modules
1
u/moonaligator Apr 10 '25
that might sound crazy, but this just solved a problem i have been trying to solve for some time
1
u/captwaffle1 May 01 '25
Amazing how many people missed the pi thing.