Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting separate python packages into same namespace?

I'm developing a python framework that would have "addons" written as separate packages. I.e.:

import myframework
from myframework.addons import foo, bar

Now, what I'm trying to arrange is so that these addons can be distributed separately from core framework and injected into myframework.addons namespace.

Currently my best solution to this is the following. An add-on would be deployed (most likely into {python_version}/site-packages/ like so:

fooext/
fooext/__init__.py
fooext/myframework/
fooext/myframework/__init__.py
fooext/myframework/addons/
fooext/myframework/addons/__init__.py
fooext/myframework/addons/foo.py

The fooext/myframework/addons/__init__.py would have the pkgutil path extension code:

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

The problem is that for this to work, the PYTHONPATH needs to have fooext/ in it, however the only thing it would have is the parent install directory (most likely, the above-mentioned site-packages).

The solution to this is to have extra code in myframework/addons/__init__.py which would tranverse sys.path and look for any modules with a myframework sub-package, in which case it adds it to sys.path and everything works.

Another idea I had is to write the addon files directly to myframework/addons/ install location, but then it would make development and deployed namespace differ.

Is there a better way to accomplish this or perhaps a different approach to the above distribution problem altogether?

like image 408
Dimitri Tcaciuc Avatar asked Jan 18 '09 05:01

Dimitri Tcaciuc


People also ask

How do I create a sub package in Python?

Create a file __init__.py and place it inside directory science so that it can be considered a Python package. Create sub-directories physics, chemistry, and biology and place __init__.py inside each sub-directories so that they can be considered Python sub-packages.

What is __ all __ in Python?

The purpose of __all__ is simply to define which symbols should be imported as part of a wildcard import that targets that module. If a symbol is importable through a wildcard import, it makes sense that it should be considered public.

What are the two ways to import packages in Python?

So there's four different ways to import: Import the whole module using its original name: pycon import random. Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd.

What is the difference between module and namespace in Python?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.


1 Answers

See namespace packages:

http://www.python.org/dev/peps/pep-0382/

or in setuptools:

http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages

like image 136
user190071 Avatar answered Oct 02 '22 10:10

user190071