Hi Alessandro, On Tue, Feb 12, 2013 at 2:30 AM, Alessandro Dentella <san...@e-den.it>wrote:
> Hi, > > this email aims at finding a bettere solution to a problem that was already > posted some months ago in the user list [1]. If you use namespaces in > packages to split your code in independently distributable packages as: > > jmb.bar > jmb.foo > > you end up not being able to use managements.commands from one of the 2 > packages as the mechanism that finds the packages in > django.core.management.find_management_module uses imp.find_module that > will > return a tuple containing just one path to one or the other. > > IIUC the problem arises since find_module_managements avoids to load all > modules (efficency concerns I guess) but so doing it misses the .__path__ > attribute on 'jmb' in the example that could be used for the next run of > find_module. > > I personally think that at least as a fallback django should try to import > the package (or document namespace packages are not supported). > The full answer is somewhere in between; namespace packages aren't supported, but that's not (to my knowledge) because we don't *want* to support them -- it's because nobody has spent the time to make sure they work. Although you're obviously hitting a limitation with management commands, I'd be deeply surprised if that's the only place that this problem emerges -- Django's internal tooling is generally fairly weak (due to lack of testing) when it comes to namespace packages. > The following patch works for me, but I'm sure there will be other > scenarios I'm not considering: > > while parts: > part = parts.pop() > - f, path, descr = imp.find_module(part, path and [path] or None) > > + try: > + f, path, descr = imp.find_module(part, path and [path] or > None) > + except ImportError: > + # we're probably dealing with a namespace package > + if len(app_name.split('.')) >= 2: > + mod = __import__(app_name + '.management', {}, {}, [''], > -1) > + return mod.__path__[0] > > > I'd be glad to make a patch for this but I wanted to hear if there's any > better idea. > I'm not sufficiently familiar with namespace packages to be able to comment here. However, I'm fairly certain that it will be possible to find canonical info on how to handle namespace packages elsewhere in Python's own importing tools. If namespace packages are important to you, then this can be a great way to contribute. Open a ticket describing the specific problem (if there isn't one already), and turn your snippet above into a proper patch -- the most important part of which will be test cases. Management command discovery etc are all tested features, but namespace packages aren't currently tested. Even if your import modification *isn't* the right solution, tests will still be needed. Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers?hl=en. For more options, visit https://groups.google.com/groups/opt_out.