Skip to content Skip to sidebar Skip to footer

Is It Possible To Use Two Python Packages With The Same Name?

I have a question about imports. The question might seem a bit contrived, but its purpose is to explore the limitations of using absolute imports for all imports in a package. PE

Solution 1:

The short answer is no, Python doesn't accept two packages with the same name. (There are things called "namespace packages" that let a single package be implemented over multiple directories but they require the packages involved to be set up to cooperate with each other).

The way PEP 8 discourages explicit relative imports is one of its more questionable pieces of advice, precisely because it makes it harder to rename a package to avoid naming conflicts. If the two packages used relative imports, you could just rename one of them or nest it inside another package and be done with it.

import aliases won't help you here, because it is the name that ends up in sys.modules that matters, and that uses the name of the module as imported, rather than the name that ends up being bound in the importing module.

If you wanted to get really exotic, you could write your own importer (see PEP 302 and the 3.x importlib documentation). If you decide to go that far, you can do pretty much anything you want.

Solution 2:

My initial tests (in Python 2.6 & 3.1) suggest the following may work:

import sys, re

import foo as foo1
for k in sys.modules:
    if re.match(r'foo(\.|$)', k):
        newk = k.replace('foo', 'foo1', 1)
        sys.modules[newk] = sys.modules[k]
        # The following may or may not be a good idea#sys.modules[newk].__name__ = newkdel sys.modules[k]

sys.path.insert(0, './python')
import foo as foo2
for k in sys.modules:
    if re.match(r'foo(\.|$)', k):
        newk = k.replace('foo', 'foo2', 1)
        sys.modules[newk] = sys.modules[k]
        # The following may or may not be a good idea#sys.modules[newk].__name__ = newkdel sys.modules[k]

However, I only tested this against very simple packages and only tried it as a curiosity. One problem is it probably breaks reload. Python isn't really designed to handle multiple packages with the same top-level name.

At this point, I'm tentatively going to say that it's not possible in the general case, though it's possible under certain limited circumstances but it's very brittle.

Solution 3:

No, but you can use keyword "as" and rename the package name as you wish. EG:

from tableaudocumentapi importWorkbookas table

Solution 4:

Actually, you should use namespaces (packages) to separate properly what modules you want to end up using. In your above code.

/pkg1
 mod1 - can just import mod2
 mod2.py
 __init__.py

/pkg2
 mod1 - can just import mod2
 mod2.py
 __init__.py

And at rest of the places you should do import pkg1.mod1 or import pkg2.mod1 as desirable.

Post a Comment for "Is It Possible To Use Two Python Packages With The Same Name?"