Is it possible to override a module with a class ?
Let me explain,
Here is the filesystem
foo
|-- bar
| |-- __init__.py
| |-- config.py
|-- __init__.py
Is it possible to override config.py from foo.bar.__init__.py so that whenever someone imports foo.bar.config they are importing overriden config instead of the version from config.py ?
If yes how do I do it ?
Ps: I will be overriding config.py based on certain conditions.
asked Sep 21, 2013 at 17:20
Gautam
7,97814 gold badges69 silver badges106 bronze badges
1 Answer 1
Just define a class in foo/bar/__init__.py with the name config and that will be imported. But why don't you just change the config.py?
In [4]: !ls foo
__init__.py bar/
In [5]: !ls foo/bar
__init__.py config.py
In [6]: !cat foo/bar/__init__.py
class config(object):
pass
In [7]: from foo.bar import config
In [8]: type(config)
Out[8]: type
$ echo "" > foo/bar/__init__.py
$ ipython
In [1]: from foo.bar import config
In [2]: type(config)
Out[2]: module
answered Sep 21, 2013 at 17:32
Viktor Kerkez
46.8k13 gold badges109 silver badges88 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
foo/bar/__init__.py, why can't you simply updatefoo/bar/config.py?