Avoiding name clashes when installing packages in Python
Asked Answered
M

0

6

When installing packages from PyPI, you have to use the name of the project, which is different from the name of the top-level packages which you will actually import. A clear example is pyserial and serial, which get installed using:

pip install serial
pip install pyserial

But both are used with something like:

import serial

If you explore the site-packages folder, you see that the contents are the combination of both packages, and, of course, the files get overwritten by the latest to be installed, giving unpredictable results.

Is there a way of avoiding this kind of name clashes when installing packages in Python? Imagine you would like to use both pyserial and serial, how would you then install them?

Madelyn answered 10/1, 2020 at 13:50 Comment(8)
There is no way. At least no easy way. https://mcmap.net/q/1918962/-resolve-conflicting-package-names-in-python/11138259Liturgy
I think, this is a better link: #27532612Madelyn
So, this is, potentially, a nightmare, because it can easily affect the dependencies of other packages within the same environment...Madelyn
This is a bad situation indeed. I wonder how these issues don't show up more often. Maybe packages should have namespaces (like zope). For your own code you can somehow get around it, for example by using importlib directly. But if you have dependencies A and B, where A wants to import serial from the project serial and B wants to import serial from the project pyserial, then it's way trickier. Maybe somehow by further hacking into importlib there is a way to detect which module is trying to import serial and then serve the right one...Liturgy
Having a central authority, like PyPA, it is a pity that this was not addressed earlier, and I am honestly shocked that this doesn't show up as a severe problem more often. It means that just by chance packages use different namespaces, unless you use packages from different worlds (like pyserial and serial), but I wonder with frameworks like Django with several projects called django-something which define a namespace something if this wouldn't happen more often... Interesting discussion nonetheless.Madelyn
There is the (weak and loose) convention of having just one top level package per project, with the package bearing the same name as the project. That could be good enough, but it is obviously not always followed (setuptools, beautifulsoup, pyserial, etc.). There is this discussion about Namespace support in PyPI but I believe it has zero effect on the actual code (the names of the top level packages). On the other hand, for ideas: discuss.python.org/t/…Liturgy
Also: bitbucket.org/davebelais/serial/issues/2/namespace-conflictLiturgy
Thanks a lot for the links! It's good to see there's people thinking about it! With serial it is easy to solve, but it can also happen with packages with a longer history and broader audiences. To my understanding, 'namespaces' cannot be reserved on PyPI, just project names...Madelyn

© 2022 - 2024 — McMap. All rights reserved.