Installing python __init__.py file from multiple KF5 frameworks

Luca Beltrame lbeltrame at kde.org
Thu May 19 22:40:55 BST 2016


In data giovedì 19 maggio 2016 19:00:07 CEST, Stephen Kelly ha scritto:

> have to be changed for the namespaced module stuff?

Sorry, you were perfectly clear. It was me who didn't get the request. ;)

>  SOME_PYTHON_2_LOCATION/PyKF5/__init__.py
>  SOME_PYTHON_2_LOCATION/PyKF5/KWidgetsAddons.so
>  SOME_PYTHON_2_LOCATION/PyKF5/KItemModels.so
>  SOME_PYTHON_3_LOCATION/PyKF5/__init__.py
>  SOME_PYTHON_3_LOCATION/PyKF5/KWidgetsAddons.so
>  SOME_PYTHON_3_LOCATION/PyKF5/KItemModels.so

Each subpackage should have its own sub-directory with an __init__.py 
importing its contents, e.g:.

SOME_PYTHON_3_LOCATION/PyKF5/KWidgetAddons/__init__.py
SOME_PYTHON_3_LOCATION/PyKF5/KWidgetsAddons/KWidgetsAddons.so

Then, 2 different cases depending if you use Python 3 or 2.

Python 3: the final result should look like

SOME_PYTHON_3_LOCATION/PyKF5/
SOME_PYTHON_3_LOCATION/PyKF5/KWidgetAddons/__init__.py
SOME_PYTHON_3_LOCATION/PyKF5/KWidgetsAddons/KWidgetsAddons.so

Python 2:

SOME_PYTHON_2_LOCATION/PyKF5/__init__.py
SOME_PYTHON_2_LOCATION/PyKF5/KWidgetAddons/__init__.py
SOME_PYTHON_2_LOCATION/PyKF5/KWidgetsAddons/KWidgetsAddons.so

The __init__.py should contain only 

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

This will fix it from the Python side. With regards to the __init__.py,

Alternatively, to avoid having a __init__.py in Python 2 too (and clashes 
among different packages), one could ship a .pth file which declares the 
namespace package and sets the import path accordingly (this should end in 
SOME_PYTHON_2_LOCATION/PyKF5.KWidgetAddons-VERSION-nspkg.pth). I'm not sure 
how to do that manually though, as usually these things, for "normal" Python 
packages, are handled directly by "setuptools" (using the "namespace_packages" 
parameter).

The one I have for zope.interface (Zope is basically the prototype for all 
namespaced packages) contains

import sys, types, os
p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('zope',))
ie = os.path.exists(os.path.join(p,'__init__.py'))
m = not ie and sys.modules.setdefault('zope', types.ModuleType('zope'))
mp = (m or []) and m.__dict__.setdefault('__path__',[])
(p not in mp) and mp.append(p)

Notice that I have *no* idea if a similar thing would work for this.

-- 
Luca Beltrame - KDE Forums team
GPG key ID: A29D259B
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/distributions/attachments/20160519/90e515a2/attachment.sig>


More information about the Distributions mailing list