Modify a subpackage behavior

11 views
Skip to first unread message

Chong Li

unread,
Aug 3, 2014, 11:49:37 AM8/3/14
to django...@googlegroups.com

I am running into a problem that I need to change a subpakcage of a big third party package in django. the big package, lets say foo, includes many subpackages, and I want to modify one of them, lets say bar. After weighing many options, I decide to dynamically change sys.modules when my django project starts. What i do is like this:

INSTALLED_APP=(...)
import sys, myproject.mybar #mybar is the the modified version
sys.modules['foo.bar']=myproject.mybar

This works fine in most cases, and whenever "from foo.bar import x,y,z" happens, it can be redirected to myproject.mybar. However, somewhere in the foo package, there is an import syntax like

import foo.bar.x
user = foo.bar.x.get_user(request)

This causes a "'module' object has no attribute" exception. Mybar package does have x module and get_user() method, and I am not sure why this happened. Anyone can help me to point me out what i miss here, also any suggestion to do what I am trying to achieve (modifying a subpackage) in a more pythonic and djangonic way?

P.S. the foo package is really big, and I dont want to include all of it in myproject. Thank for your help in advance Python community ( :

Reply all
Reply to author
Forward
0 new messages