0

I am creating a functional "component" which has the following structure right now:

from config_file import *
def FunctionalComponent():
  foo(a_from_config)
  bar(b_from_config)
  ...

but want to convert it into something like

def functional_component(module_file):
  <do something with `module_file` which imports all the variables>
  foo(a_from_config)
  bar(b_from_config)
  ...

which can be called by the end user as

functional_component(module_file = "path_to_config_file.py")

but am not able to find a good way to do so. I tried using

def FunctionalComponent(module_file):
    from module_file import a_from_config
    print(a_from_config)

and calling it as (i wasn't sure how to pass the string (path to file) and import it, so tried passing the imported module itself. For the final function, passing a path(string) as a parameter is needed)

import module_file
FunctionalComponent(module_file)

but that gave ModuleNotFoundError: No module named 'module_file'

Any advice would be highly appreciated, thanks :)

Eagle
  • 318
  • 4
  • 16
  • 1
    Use [`importlib`](https://docs.python.org/3/library/importlib.html) – Barmar Jul 28 '21 at 23:54
  • @Barmar Oh my god thank you very much, this was the exact thing needed! Could you maybe add it as an answer and I can accept it? Or should I write one with what I did. – Eagle Jul 29 '21 at 00:04

0 Answers0