For fun, you could add this to Python and I think it would it cover a lot of edge cases?<p>You would need:<p>A function <i>v_tree_install(spec)</i> which installs a versioned pypi package like “foo=3.2” and all its dependencies in its own tree, rather than in site-packages.<p>Another pair of functions <i>v_import</i> and <i>v_from_import</i> to wrap importlib with a name, version, and symbols. These functions know how to find the versioned package in its special tree <i>and</i> push that tree to sys.path before starting the import.<p>To cover the case for when the imported code has dynamic imports you could also wrap any callable code (functions, classes) with a wrapper that <i>also</i> does the sys.push/pop before/after each call.<p>You then replace third party imports in your code with calls assigning to symbols in your module:<p><pre><code> # import foo
foo = v_import(“foo==3.2”)
# from foo import bar, baz as q
bar, q = v_from_import(
“foo>=3.3”,
“bar”,
“baz”,
)
</code></pre>
Finally, provide a function (or CLI tool) to statically scan your code looking for <i>v_import</i> and calling <i>v_tree_install</i> ahead of time. Or just let <i>v_import</i> do it.<p>Edit: …and you’d need to edit the sys.modules cache too, or purge it after each “clever” import?