Python Namespace package as extension to existing package -


is possible add namespace/path of existing package not namespace package?

lets have extisting external package named package.

i use in project.py this:

from package.module import class 

is possible create package called extension namespace of package importable package.extension?

from package.module import class package.extension.module import extensionclass 

is possible install both packages using pip/setuptools without adding monkey patches project.py in want import both package , package.extension?


partial solution

i've been able achieve need in 2 ways: modifying original package , monkey patching in project.py

structure:

./test.py ./demo/test.py ./demo/__init__.py ./extension/demo/__init__.py ./extension/demo/extension/test.py ./extension/demo/extension/__init__.py ./extension/__init__.py 

contents of ./test.py:

import demo.test demo.test.hello()  import demo.extension.test demo.extension.test.hello() 

partial solution 1 - modify original package

i've modified demo/__init__.py contains:

import pkgutil  __path__ = pkgutil.extend_path(__path__, __name__) 

execution example:

$ export pythonpath=extension $ python test.py  hello! i'm demo. hello! i'm extension. 

partial solution 2 - modify original package

i've modified ./test.py contains monkey patch:

import pkgutil import demo demo.__path__ = pkgutil.extend_path(demo.__path__, demo.__name__)  import demo.test demo.test.hello()  import demo.extension.test demo.extension.test.hello() 

execution example:

$ export pythonpath=extension $ python test.py  hello! i'm demo. hello! i'm extension. 

the problem (again)

one of solution requires owner of original package allow extensions, other solution requires monkey patching. there possibility package installed via setup.py/pip not require chaning of original package or monkey patching?

this may not helpful, can check flask , of its extensions sources. flask extensions works describe, eg.:

from flask import flask flask.ext.sqlalchemy import sqlalchemy 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -