Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a python environment, how to import a package from a specific version?

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?

like image 463
tribbloid Avatar asked Oct 26 '25 05:10

tribbloid


1 Answers

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)
like image 132
Gianluca Avatar answered Oct 27 '25 18:10

Gianluca