I am making a server that listens on a socket. Whenever a new request comes in, the server spawns a new instance of handle_request. Each instance of handle_request.py imports the relevant handler from request_handlers.
server.py
handle_request.py
request_handlers
|_handler_beta
|_handler_1
|_handler_2
While handlerX is a module, request_handlers is not a package. The modules are self-contained and reloaded on each request. The modules may be added, modified or dropped while the program is running.
Question: What is the way to import a module from an arbitrary directory?
Doing my homework, I saw that most questions deal with packages, even one is titled “python: import a module from a folder“. Hence I believe this question is distinct. The architecture has been simplified; and yes, I am considering pre-forking with reload on file modification.
Create
__init__.pyin the directory. That makes it a package. If you’re scanning the directory for .py files, you’ll probably then want to skip__init__.py.Then, you can import them with
__import__('request_handlers.' + module, fromlist=[''])(the fromlist is important, otherwise you’ll getrequest_handlersrather than the appropriate module).One way of doing it without an
__init__.pywould be to put therequest_handlersdirectory insys.path, but that then makes name clashes with other modules possible. Another way would be withexecfile. You can research that more if you want to. I’d do it (and have done it before) the package/__import__ way.