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?
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.
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.
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.
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.
See namespace packages:
http://www.python.org/dev/peps/pep-0382/
or in setuptools:
http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With