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:
-
ingest the package source/byte codes of both
library (v1.0)
andlibrary (v2.0)
, generate 2 cryptographic hash for each, and install them under 2 different paths that depends on their respective hashes. -
use a code generator to rewrite
feature_A
andfeature_B
such that they import from new paths that depends on cryptographic hashes, and install the rewritten versions. -
use the code generator to rewrite
program
such that it imports from the new paths where the rewrittenfeature_A
andfeature_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
I would install the different package versions in different
PYTHONPATH
, e.g.or
and the same for the other version of the package to be installed in another path
insertherepath2
. The two installations in differentPYTHONPATH
s still can access other commonly installed packages in the main python path.To use the different packages within
python
:and equivalently to use the other version:
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
python pkg_resources may help you .
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.