Тёмный

Python Plugin System: Load Modules Dynamically With "importlib" 

Real Python
Подписаться 194 тыс.
Просмотров 36 тыс.
50% 1

Опубликовано:

 

24 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 37   
@DaniEIdiomas
@DaniEIdiomas 2 года назад
Thanks to this video, I was able to assign an object execution to a button in KiVy. Thanks!
@elyezer
@elyezer 7 лет назад
Worth mentioning about the getattr and hasattr which can help you get an attribute (e.g. the plugin class) or check of the module has the attribute respectively. Those can help you provide feedback and tell users that something expected is not found. Both can also be used for checking for the execute method. I like how you presented the import_module and it is very hand when you need to import things at run time.
@snek3205
@snek3205 7 лет назад
Sorry for the stupid question, but why don't you need an __init__.py file to import plugins.one? In the documentation it says you need an __init__.py file inside the folder.
@andreas_bergstrom
@andreas_bergstrom 5 лет назад
Since Python 3.3+ you no longer need a empty __init__.py file just to make a folder into a package. If you want a cleaner API you still have to use the __init__py though, since otherwise still have to use package.module.class when you perhaps want it to be only package.class for smaller packages.
@gui1542
@gui1542 7 лет назад
Awesome video Dan! The production is really beautiful! May I make a request? Recently, since python3.4, the pathlib module has been adopted through out the standard library for path handling. I think the community would really benefit from some content that explains how/when to use path lib, in comparison to the os.path.
@realpython
@realpython 7 лет назад
Glad you liked it! Thanks for the suggestion! :-)
@insanecbrotha
@insanecbrotha Год назад
Cool. This is like the factory method pattern but on the module level, right?
@gettingpast4391
@gettingpast4391 5 лет назад
Very handy exactly what I was looking for thanks
@realpython
@realpython 5 лет назад
I'm glad it was what you are looking for. :-)
@iilillillilillli
@iilillillilillli 24 дня назад
what is differ to use 'as'? from PACKAGE import MODULE as MD. In this way, I dont need to change the module name in the code base, too.
@kdkulakov
@kdkulakov 2 года назад
Can you tell me please, if right understood, we load every time just one plugin by name. How load two plugins?
@nravikiran4661
@nravikiran4661 2 года назад
Thank you soo much, Helped me a lot
@bennguyen1313
@bennguyen1313 4 года назад
I'd like to have my python file call the __main__ from another file. However, the other __main__ expects command-line arguments... how can I call it and wait for until it returns/exits before continuing my file's execution?
@hawwestin
@hawwestin 7 лет назад
I think it is the way to do a proper Translation switcher in my desktop app :) directory witch stuff and import witch one user need.
@Lorant1984
@Lorant1984 3 года назад
Superb! Thank you.
@graingerous16
@graingerous16 3 года назад
Great tutorial, thank you!
@jokerssd
@jokerssd 5 лет назад
first off, great video, just what im looking for im trying to build a python script load that open up a window dialog to allow user to choose which file to load so i have a question,i notice you said that u want to load relative to current folder so is something like this possible? module=importlib.import_module(modname,Path(r'C: a\Dropbox\My python files\guide'))
@AlexCaranha
@AlexCaranha 4 года назад
Very good. Thanks a lot.
@swadhikarc7858
@swadhikarc7858 5 лет назад
Nice place to start learning importlib library. But this module has far more amusing features
@s_kh4414
@s_kh4414 3 года назад
Great. Thank you a lot
@antebilic9259
@antebilic9259 7 лет назад
Thanks, Dan. Just wondering, couldn't one achieve a similar effect with eval or exec?
@realpython
@realpython 7 лет назад
Good question-I'm not a big fan of eval because it makes debugging more difficult. It would certainly be possible to hack together plugin loading using eval (load the code from a file, run it through eval) but I'd probably lean towards importing things as a module.
@pavelow36
@pavelow36 4 года назад
Thank you for this nice tutorial. My problem is how can I get a permanent global handle for the module on every iteration? In your example, I need to use the module outright inside the loop after it gets assigned the handle 'plugin_module' in L7 of app.py. Because on the next iteration of the loop, 'plugin_module' will not be anymore one, but it will be two. I've tried assigning to globals()[plugin_module] on each loop but it seems to be not working either. Thanks!
@samuelhulme8347
@samuelhulme8347 3 года назад
Well you could have a list above the loop and when in that loop append to module to it. Then elsewhere for another module action loop the list and do it then
@jarvo7723
@jarvo7723 3 года назад
Thanks
@timurbabadjanov9115
@timurbabadjanov9115 6 лет назад
Awesome! Thank you Dan!
@realpython
@realpython 6 лет назад
You're welcome! :-)
@mandarautade5053
@mandarautade5053 6 лет назад
can you make a video on Flask Plugins.....?
@realpython
@realpython 6 лет назад
Thanks for the suggestion!
@iamjameswong
@iamjameswong 7 лет назад
Link to wallpaper? :)
@realpython
@realpython 6 лет назад
Sure thing! → realpython.com/products/pythonic-wallpapers/
@dannymurphy2823
@dannymurphy2823 7 лет назад
always the best
@realpython
@realpython 7 лет назад
Thanks! :-)
@Xenonandplutonium
@Xenonandplutonium 10 месяцев назад
🖤🖤✌️
@ilyam1425
@ilyam1425 6 лет назад
No-no, this is shit man, sorry. Dynamicly is when i does not know any plugin or class module name.
@gettingpast4391
@gettingpast4391 5 лет назад
You should watch it again. At 9:22, you simply replace PLUGIN_NAME with a directory scan of the plugins directory. He's defined it only for the example.
@TheFlexXMLG
@TheFlexXMLG 4 года назад
really cool thank you
Далее
Пресс-конференция перед UFC 308
49:51
Why the Plugin Architecture Gives You CRAZY Flexibility
24:06
Optional Arguments in Python With *args and **kwargs
10:44
Type-Checking Python Programs With Type Hints and mypy
10:50
Пресс-конференция перед UFC 308
49:51