Version conflicts for Python apps but not for interactive development

419 Views Asked by At

I have a package P that depends on a package A. Package A depends on packages B and C==3. At the same time, Package B depends on C==4.

This creates conflicts and an akward error message is shown in red every time I pip install packages P or A.

Now, I know I can run packages P and A just fine if I import them in an interactive terminal or Jupyter notebook and I call the functions and classes I need. However, package P has a command line application, which raises an error as long as you have the install conflicts inherited from package A.

This error is not raised by me, it is raised by the Python interpreter alone (I think), since I am not calling any new functionality compared to when I use P as a library. In fact, my CLI is a class wrapped by fire, which I can call without problems in an interactive session.

The error trace shows pkg_resources.ContextualVersionConflict in the end, which I never call in P.

Given that I can only control what happens in package P, is there a way for make it work directly as a command line app?

I am also interested to know what is happening under the hood.

Bytheway, I am always installing P in a new Conda environment.

For package B there is only one version available, not multiple versions.

Thanks!

1

There are 1 best solutions below

3
On

From what I understood the version of B is not constrained by A, so basically any version of B would be acceptable. Now, maybe there is a version of B that has C==3 in its dependencies. If such a version of B exists, let's say it's B==5, then the following could work:

path/to/pythonX.Y -m pip install P B==5

If it does indeed work, for a long term solution, you might want to try one of the following:

  • Set a B==5 constraint on your project P, since you have control over it. I probably wouldn't recommend it though, since B is not actually a direct dependency of P.
  • Use a constraints.txt file containing B==5 and call pip with the --constraint option:
    • path/to/pythonX.Y -m pip install --constraint constraints.txt P

Additionnally I would recommend giving pip's new, experimental dependency resolver a try. It might be better at finding the right combination or projects to install in such situations.

path/to/pythonX.Y -m pip --unstable-feature=resolver install P

See this answer for details: