I would like to update one module in a python package with my own version of the module, with the following conditions:
- I want my updated module to live outside of the original package (either because I don't have access to the package source, or because I want to keep my local modifications in a separate repo, etc).
- I want
import
statements that refer to original package/module to resolve to my local module
Here's an example of what I'd like to do using specifics from django, because that's where this problem has arisen for me:
Say this is my project structure
django/
... the original, unadulterated django package ...
local_django/
conf/
settings.py
myproject/
__init__.py
myapp/
myfile.py
And then in myfile.py
# These imports should fetch modules from the original django package
from django import models
from django.core.urlresolvers import reverse
# I would like this following import statement to grab a custom version of settings
# that I define in local_django/conf/settings.py
from django.conf import settings
def foo():
return settings.some_setting
Can I do some magic with the __import__
statement in myproject/__init__.py
to accomplish this? Is there a more "pythonic" way to achieve this?
Update - Why do I want to do this
Here's the scenario where I think this makes sense.
- I am launching a django powered website on a server that has django pre-installed globally. I can't modify the actual django source, in this case.
- My django project uses 3rd party reusable apps, and I don't want to change
imports
in all of those apps to import, for instancemycustomsettings
. I want to leave the reusable apps blissfully ignorant that I have changed the implementation of django.conf.settings.