Monday, 5 August 2019

Python Namespace Packages in Python3

The topic of namespace packages seems a bit confusing for the uninitiated, and it doesn't help that prior versions of Python have implemented it in a few different ways or that a lot of the Q&A on StackOverflow are dated. I am looking for a solution in Python 3.5 or later.

The scenario:

I'm in the process of refactoring a bunch of Python code into modules and submodules, and working to get each of these projects set up to operate independently of each other while sitting in the same namespace.

We're eventually going to be using an internal PyPi server, serving these packages to our internal network and don't want to confuse them with external (public) PyPi packages.

Example: I have 2 modules, and I would like to be able to perform the following:

from org.client.client1 import mod1
from org.common import config

The reflected modules would be separate as such:

Repository 1:

org_client_client1_mod1/
  setup.py
  mod1/
    __init__.py
    somefile.py

Repository 2:

org_common_config/
  setup.py
  config/
    __init__.py
    someotherfile.py

My Git repositories are already setup as org_client_client1_mod1 and org_common_config, so I just need to perform the setup on the packaging and __init__.py files, I believe.

Questions:

#1

With the __init__.py, which of these should I be using (if any)?:

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

Or:

import pkg_resources
pkg_resources.declare_namespace(__name__)

#2

With setup.py, do I still need to add the namespace_modules parameter, and if so, would I use namespace_modules=['org.common'], or namespace_modules=['org', 'common']?

#3

Could I forgo all of the above by just implementing this differently somehow? Perhaps something simpler or more "pythonic"?



from Python Namespace Packages in Python3

No comments:

Post a Comment