0

I want to import a file as python module with importlib. This file has no .py but .cfg extension. It contains python code.

[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import imp
>>> imp.load_source('myconfig', 'conf.cfg').foo
'bar'
>>> import importlib
>>> importlib.import_module('conf.cfg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'conf'

I can do it with imp.load_source but it is deprecated. Do it with importlib.import_module convert . as module exploration.

How to import a conf.cfg file with importlib ?

martineau
  • 119,623
  • 25
  • 170
  • 301
bux
  • 7,087
  • 11
  • 45
  • 86
  • I need to ask, why do you have a `.cfg` that contains python code? is it that it contains config code but happens to use the same assignment syntax as python? – Tadhg McDonald-Jensen Dec 09 '16 at 15:56
  • @TadhgMcDonald-Jensen conf.cfg is not part of my application. But my application want to read it. – bux Dec 09 '16 at 15:59
  • 2
    You could create a module using `types.ModuleType()`, then execute conf.cfg's code in the new module's dict, and even insert the module in `sys.modules` if you want to. This would avoid both imp and importlib. – Gribouillis Dec 09 '16 at 16:00
  • Use a dictionary and `ast.literal_eval(whole_file_or_split_lines)` – dsgdfg Dec 09 '16 at 16:01
  • @dsgdfg literal eval won't parse assignment statements, you would still need to do a lot of parsing yourself to work quite like that. – Tadhg McDonald-Jensen Dec 09 '16 at 16:03
  • 1
    @bux if the file isn't part of your application I would have a whole other range of concerns about executing it as arbitrary code.... either way `importlib` is essentially an implementation of the `import` mechanism which doesn't support loading module by direct filename so I don't think this is possible with `importlib`. – Tadhg McDonald-Jensen Dec 09 '16 at 16:09
  • @TadhgMcDonald-Jensen did you see any configuration parser without a lot of parsing ? – dsgdfg Dec 09 '16 at 16:15
  • Seems to me you could hack this simply by copying the `conf.cfg` file into another with a `.py` extension, and then use `importlib` to import that like any other normal module. This should work without you needing to know all the internal details of the `import` mechanism (so it would be easier to maintain as well as less work to implement in the first place). – martineau Dec 09 '16 at 16:40
  • @martineau I think it will be fine like this. Thank's – bux Dec 12 '16 at 07:59

0 Answers0