I'm trying to make a command handler, here's some code:
from importlib import import_module from os import path, listdir client.commands = [] # where the commands are stored directory = '.\\src\\exec\\files' for command in listdir(directory): filepath = path.join(directory, filename) if path.isdir(filepath) and 'lib' not in filepath: # check if file is a directory log_commands(directory=filepath) # make recursive logging elif filename.endswith('.py'): command_name = filename.split('\\')[-1] module_name = devutils.filepath2module(filepath, True, True) # convert file path to module path client_utils.commands[command_name] = import_module(module_name) # puts module in a new item
The code works fine, but the import_module function imports only the entire function. I was wondering if there's some library that imports specific objects from the module, like:
from module import object
But in a dynamic way, I searched a little bit and couldn't find anything. (Sorry for bad english)
https://stackoverflow.com/questions/65839170/is-there-a-way-to-dynamically-import-python-objects January 22, 2021 at 12:08PM
没有评论:
发表评论