Code

Opened 15 months ago

Closed 14 months ago

Last modified 14 months ago

#20344 closed Bug (duplicate)

Allow django to discover management commands from namespace packages.

Reported by: eire1130 Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords:
Cc: bhuztez@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Certain third party package maintainers may desire to have their apps in a namespace package. If those packages contain management commands, Django might not be able to discover them.

For example, if you have a namespace package that looks something like this:

site-packages/namespace/space/package1
site-packages/namespace/space/package2

Django will not be able to discover the packages during management registration (and in fact, could raise errors depending on the management command). If Django fails to locate a package during this process, it fails silently, which could be confusing for some developers.

Package discovery fails for one primary reason:

  • 1. Namespace and namespace/space directories above do not necessarily need init.py files located in their directory.

So for demonstration, import namespace.space.package1 works while import namspace and import namespace.space will not work.

The workaround here is just to allow for a direct import (if it succeeds) and take the path from that module for django to register the management command.

This solution should work in most situations where the namespace package is installed "normally" and can be found in the python path.

Branch is here: https://github.com/eire1130/django/tree/namespace_management_imports
Commit is here: https://github.com/eire1130/django/commit/3d495addc567e617eaef2b107fea6edaad161288

If approved, I will send the pull request.

Attachments (0)

Change History (4)

comment:1 Changed 15 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Isn't this a duplicate of #14087?

(IMO using namespace packages is just asking for trouble; I forbid their use; I'm at best -0 on this change.)

comment:2 follow-up: Changed 15 months ago by eire1130

It does appear to be a dup, though I prefer my fix.

For what it's worth, I agree with you on namespace packages, but some of us need to use namespace packages for reasons that are out of control.

comment:3 Changed 14 months ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

comment:4 in reply to: ↑ 2 Changed 14 months ago by bhuztez

  • Cc bhuztez@… added
  • Easy pickings unset

Replying to eire1130:

It does appear to be a dup, though I prefer my fix.

For what it's worth, I agree with you on namespace packages, but some of us need to use namespace packages for reasons that are out of control.

the docstring says

Determines the path to the management module for the given app_name,
without actually importing the application or the management module.

obviously, your patch would import application module

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.