简体   繁体   中英

Django: I want to override exactly one module in a contributed library to patch one tiny thing

There is exactly one module in the "pinax" contributed library – which is essential to my project – that I wish to override. And that one module is site-packages/pinax/templatetags/templatetags/shorttimesince_tag.py , which references the django.utils.tzinfo module which no longer exists in current Django. (Having been replaced by django.utils.timezone .

I would like to "override" this particular module by arranging things so that Python will encounter it first. Well, I tried unsuccessfully to put this into the wsgi file of the application:

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "overrides")))

I then created an overrides/pinax/templatetags/templatetags directory tree with an __init__.py file in each.

I don't think that Django gets this far, before it dies with an import error. (Because a "print()" statement didn't produce any output.)

But, I feel that something along these lines ought to work. Because I really don't want to have to absorb this entire contributed library into my application, literally just to change one line of source.

Any ideas? Am I just barking up the wrong tree? *(PS: I am also using another library, the "machina" forum, which does include a very nice built-in "override" mechanism. But I don't know exactly how it works, and I haven't yet "dumpster-dived" to find out...)

Tested it with local files, and it seems, when you override a package by creating a package in override with same name and __init__.py inside, it overrides the whole original package, not just the files you've created. So you have to copy all the files from that package.

And if you don't specify __init__.py , python doesn't recognize your package and imports the original.

I think, the best way here is to fork the original package's repo, change it, and use the fork in your project. Also consider creating a PR to the original repo if you think your changes might be useful to others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM