0

I am wondering whether it is possible to work with several versions of the same Python package in the same Python session.

For instance, say I want to investigate possible differences between Gensim-3.7.3 and Gensim-3.8.1, is the only way then to make two virtualenvs, start two separate Python sessions, save the results, and load the results into one of the sessions (or a third sessions)?

Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43
  • 1
    At the very least I'd say you'll need a `venv` for each version of the package. Then my approach would be to access the specific `venv` with `subprocess` or `multithreading`. You could run the same script against the `python` interpreter in the respective `venv` and compare the results. – r.ook Oct 17 '19 at 13:19

1 Answers1

0

With importlib, a virtualenv, explicit paths and borrowing an answer from @sebastian-rittau at How to import a module given the full path?, two versions of a module can be loaded. Example:

from importlib.util import spec_from_file_location, module_from_spec
from os.path import expanduser

name = "gensim"
paths = [
    expanduser('~/envs/gensim-3.8.0/lib/python3.6/site-packages/gensim/__init__.py'),
    '/usr/local/lib/python3.6/dist-packages/gensim/__init__.py',
]

gensims = []
for path in paths:
    spec = spec_from_file_location(name, path)
    gensim = module_from_spec(spec)
    spec.loader.exec_module(gensim)
    gensims.append(gensim)

for gensim in gensims:
    print(gensim.__version__)

The result is:

3.8.0
3.7.3
Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43