1

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
1
  • If you can modify foo/bar/__init__.py, why can't you simply update foo/bar/config.py? Commented Sep 21, 2013 at 17:28

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.