Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different python packages with the same top level name [duplicate]

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 *
like image 296
Madden Avatar asked Sep 16 '25 20:09

Madden


1 Answers

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.

like image 146
spyralab Avatar answered Sep 19 '25 00:09

spyralab