Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle python packages with conflicting names?

I'm using two python packages that have the same name.

  • http://www.alembic.io/updates.html
  • https://pypi.python.org/pypi/alembic

Is there a canonical or pythonic way to handle installing two packages with conflicting names? So far, I've only occasionally needed one of the packages during development/building, so I've been using a separate virtualenv to deal with the conflict, but it makes the build step more complex and I wonder if there isn't a better way to handle it.

like image 766
Brendan Abel Avatar asked Dec 17 '14 18:12

Brendan Abel


People also ask

How do you resolve a name conflict in Python?

The easiest and most sane way to do it is to refactor you project and change the name of the file. There are probably some way strange way around this, but I would hardly consider that worth it, as it would most likely complicate your code, and make it prone to errors.

What are name clashes in Python?

By default, Python paths are relative to the main script that is being executed. On Python 2.7 and older, imports are also relative to the module doing the importing. This means if you get weird import errors, check for name clashes. The higher-level lesson here is that absolute imports are easier to understand.

Does pip resolve dependencies?

Unfortunately, pip makes no attempt to resolve dependency conflicts. For example, if you install two packages, package A may require a different version of a dependency than package B requires.

Can Python package names have hyphens?

Python packages and modules can not use hyphens, only underscores. This section of PEP-8 gives us guidance: Package and Module Names: Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.


1 Answers

You could use the --target option for pip and install to an alternate location:

pip install --target=/tmp/test/lib/python3.6/site-packages/alt_alembic alembic

Then when you import in python, do the first as usual and for the alt do an import from that namespace like this:

import alembic  # alembic.io version
from alt_alembic import alembic as alt_alembic  # pip version

Then when you're making calls to that one you can call alt_alembic.function() and to the one that isn't in PyPi, alembic.function() My target path has /tmp/test as I was using a virtual env. You would need to replace that path with the correct one for your python installation.

like image 50
Michael Robinson Avatar answered Sep 21 '22 17:09

Michael Robinson