1

If I install two python packages with the same top directory level name but different package names (as defined by the setup.py or perhaps another method of identification), will that conflict or will the 'namespaces' be merged?

For example, if I have the following structure:

repo1
    mypkg/
        __init__.py
        compiler/...
setup.cfg -> name=repo1

repo2
    mypkg/
        __init__.py
        runner/...
setup.cfg -> name=repo2

Can I install both of these without clashing? If yes, can I then import them like this:

from mypkg.runner import *
from mypkg.compiler import *
Madden
  • 1,024
  • 1
  • 9
  • 27

1 Answers1

4

By default python would recognise only one of your two packages with one overwriting the other in the session.

If you put the following line in both your init.py files in the mypkg packages, you merge the packages together.

__path__ = __import__("pkgutil").extend_path(__path__, __name__)

What will happen is that instead of overwriting one package with another python puts the content of the packages into the same mypkg namespace.

However be warned, clashing modules or sub packages are not resolved automatically.

So if you create a runner sub package in both mypkg packages only one of the runner packages will be loaded.

spyralab
  • 161
  • 6
  • Thank you, this helped me quite a lot! I was thinking about entry points in setuptools to "dynamically" append packages to a given parent package, however this is a far easier way! – janus235 Oct 07 '22 at 18:35