0

I have an old system that I’m trying to replicate. It is based on an old Debian Wheezy (7.1) system and uses Apache 2.2.22 and libapache2-mod-wsgi 3.3, in daemon mode, and Python 2.7.3. The WSGI application uses packages from a virtualenv, created with access to the global site-packages modules. It uses two subpackages from the same namespace package, that are installed by the system in different places:

  • repoze.who, installed in /usr/lib/pymodules/python2.7/
  • repoze.lru, installed in /usr/lib/python2.7/dist-packages/

My problem is that it does not work on the replicated system: the application fails to import repoze.who. On the other hand, on the original system, it works file and successfully imports both subpackages.

I understand that it’s not supposed to work since, in normal configuration, Python does not expect a package to be split in several places.

I read the python import different subpackages with the same root packge name and different locations question and its answer. If I modify both repoze/__init__.py files as suggested, it works fine on the replicated system.

But I checked both files on the original system, and they are empty.

I added a few print instructions in the WSGI application, to investigate the original system. sys.path looks fine and similar on both systems, with /usr/lib/python2.7/dist-packages before /usr/lib/pymodules/python2.7 on both. Surprisingly, the repoze package is imported from /usr/lib/pymodules/python2.7 on the original system, while it is imported from /usr/lib/python2.7/dist-packages on the replicated system. Moreover, repoze.__path__ contains both locations on the original system, while it only contains /usr/lib/python2.7/dist-packages/repoze on the replicated system.

Last but not least, if I run the virtualenv’s python interpreter on the original system, it fails to import the repoze.who package while it imports repoze.lru file, which looks like a normal behaviour. Hence, there must be something special with mod-wsgi or the application.

I guess something “clever” was done to allow the WSGI application to load both packages from different locations, but I have no idea what. Any suggestion is welcome.

user2233709
  • 173
  • 1
  • 7

1 Answers1

0

repoze is using namespace packages.

From its setup.py:

      namespace_packages=['repoze', 'repoze.who', 'repoze.who.plugins'],
  • See PEP 420 for the modern way to build namespace packages.
  • See pkg_resources for the old way (the approach I have personal experience with, back in the mid-2000s).
  • See the pkgutil approach when you need compatibility with existing code already using that approach.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks, that’s interesting reading about how things should be done. But it does not really answer my question. I wonder why the WSGI application works fine on my original system, while it fails to import repoze.who on the replicated system or when I run the virtualenv’s python interpreter by hand on the original system… – user2233709 Sep 30 '22 at 08:03