我想知道是否可以在同一Python会话中使用同一Python包的多个版本。
例如,说我想研究Gensim-3.7.3和Gensim-3.8.1之间的可能差异,这是制作两个virtualenv,启动两个单独的Python会话,保存结果并加载结果的唯一方法进入一个会议(或第三次会议)?
答案 0 :(得分:0)
使用importlib
,virtualenv,显式路径并在How to import a module given the full path?处借用@ sebastian-rittau的答案,可以加载模块的两个版本。示例:
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__)
结果是:
3.8.0
3.7.3