skip to Main Content

This is not a duplicate of the question:

Import python packages with different versions installed

Nor can it be solved by virtualenv/pipenv: packages that share the same name/path but has different code/version must be installed under the same environment for the program to function properly

Considering the following classic diamond dependency problem:

  • program <- feature_A <- library (v 1.0)
  • program <- feature_B <- library (v 2.0)

Assuming we have full access over the sourcecode of packages ‘program’, ‘feature_A’, and ‘feature_B’, and both feature_A and feature_B has the following code:

import library.*

In a conventional package manager like virtualenv, pip and conda, The above situation will preclude feature_A and feature_B from being used in the same project. However since python is an interpretive language, and we can use the source code of feature_A and feature_B. It should be possible to do the following things:

  1. ingest the package source/byte codes of both library (v1.0) and library (v2.0), generate 2 cryptographic hash for each, and install them under 2 different paths that depends on their respective hashes.

  2. use a code generator to rewrite feature_A and feature_B such that they import from new paths that depends on cryptographic hashes, and install the rewritten versions.

  3. use the code generator to rewrite program such that it imports from the new paths where the rewritten feature_A and feature_B are installed.

Now my question is: how difficult it is to fully automate this process? Is a weak artificial intelligence required? Or it can be accomplished using pure functional logic?

4

Answers


  1. I would install the different package versions in different PYTHONPATH, e.g.

    PYTHONPATH=insertherepath1 ; python setup.py install --prefix=insertherepath1
    

    or

    PYTHONPATH=insertherepath1 ; pip install --install-option="--prefix=insertherepath1" package==v1
    

    and the same for the other version of the package to be installed in another path insertherepath2. The two installations in different PYTHONPATHs still can access other commonly installed packages in the main python path.

    To use the different packages within python:

    import sys
    sys.path.insert(0, 'insertherepath1')
    import package #gets version 1
    #maybe (depends on further dependencies) : sys.path.pop(0)
    

    and equivalently to use the other version:

    import sys
    sys.path.insert(0, 'insertherepath2')
    import package #gets version 2
    #maybe (depends on further dependencies) : sys.path.pop(0)
    
    Login or Signup to reply.
  2. Installing specific versions

    pip allows you to specify which version of a package to install using version specifiers. For example, to install a specific version of requests:

    pip install requests==2.18.4

    To install the latest 2.x release of requests:

    pip install requests>=2.0.0,<3.0.0

    To install pre-release versions of packages, use the –pre flag:

    pip install --pre requests

    Login or Signup to reply.
  3. python pkg_resources may help you .

    https://setuptools.readthedocs.io/en/latest/pkg_resources.html#workingset-objects
    
    Login or Signup to reply.
  4. I haven’t run into this scenario myself. My suggestion would be to rename the libraries so they install at different names. To me that seems like the simplest approach.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search