![]() |
|
[Python] Need help dynamically importing modules - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Need help dynamically importing modules (/showthread.php?tid=6026291) |
Need help dynamically importing modules - 4ground_daemon - Nov 19, 2019 So I'm not sure how to exactly explain it but i'm wanting to build a bot that is going to scrape a load of blog websites to get check if they have made a new blog post since the last check I did. Now because the amount of sites I want to scrape is going to be a lot (looking at 50-100) I want a way to have each blog in its own module (as it will need its own functions to scrape the site) and all of these modules will be in a separate directory. So what I want in the end is for the bot to just import all the modules in that directory rather then having to hard code each on in which I know how to do) my problem is how do I run the functions in each one of these imports because i'm not hard coding a name in? I basically want to go through each of the modules in that folder and then just run the functions in it. To write it in pseudocode I would basically say: -> import all modules in the /blogs directory -> for each module in that directory run module_name.check() -> check if latest blog post is new -> if so return the url But how do I reference module_name if i'm just importing all modules in that directory? Also just to note. All the modules will have the same function names so there will be no problems with that. Also just to clarify, another reason why I want it like this is to future proof it. It will allow me to just keep adding sites to the bot without having to touch the original file. RE: Need help dynamically importing modules - datguy.dev - Nov 19, 2019 I don't know the answer to your exact question. But what about a single library of classes instead? Wherein each "class" is essentially a library as you know it. You just have to know how to format them. Each class could have a self.name variable for their id and .check() method. RE: Need help dynamically importing modules - Charger - Nov 19, 2019 https://stackoverflow.com/a/301146 That’s exactly how RE: Need help dynamically importing modules - datguy.dev - Nov 19, 2019 (Nov 19, 2019 07:03 PM)Charger Wrote: https://stackoverflow.com/a/301146 I'd agree. But (moduleNames = ['sys', 'os', 're', 'unittest']) would fall under hardcoding. RE: Need help dynamically importing modules - Charger - Nov 19, 2019 (Nov 19, 2019 07:58 PM)datguy.dev Wrote: I'd agree. But (moduleNames = ['sys', 'os', 're', 'unittest']) would fall under hardcoding. -_- os.dir For x in files: Modulenames.append(x.name) Nachfolger RE: Need help dynamically importing modules - Wiked™ - Nov 19, 2019 I used a different approach 'importdir.py' Code #---------------------------------------------------------------------------------------------------then in my code (used as a modulated cracker) 'main.py' Code import importdira module/addon would be a simple requests that i would then repeatedly call over depending on the modules name 'modules/dyn.py' Code #!/usr/bin/python |