G Smith
G Smith

Reputation: 53

How can I import/use two different versions of a library (pytorch) in one program in Python?

I need to use two different versions of pytorch in different parts of the same python webserver. Unfortunately, I can't install them both on the same conda environment that I'm using. I've tried importing one of them from the path itself:

MODULE_PATH = "/home/abc/anaconda3/envs/env/lib/python3.7/site-packages/torch/__init__.py"
MODULE_NAME = "torch"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)

Which works fine for importing a different version than the one in the active environment, but then I run into an error when trying to import the second one (I've tried simply 'import torch' and also the same as above):

File "/home/abc/anaconda3/envs/env2/lib/python3.7/site-packages/torch/__init__.py", line 82, in <module>
    __all__ += [name for name in dir(_C)
NameError: name '_C' is not defined

Any ideas on how I can use both versions? Thanks!

Upvotes: 1

Views: 2989

Answers (1)

Ignacio Vergara Kausel
Ignacio Vergara Kausel

Reputation: 6016

In principle, importing two libraries with the same name is not possible. Sure, it might be the case that you could do some import-sorcery and manage to do it. But keep in mind that pytorch is not a straightforward Python package.

Now, even if you manage to solve this, it seems extremely strange to me that you need for your own service two different versions. Having that situation will just be a headache for you in the long run. My advice would be to reconsider how you're doing it.

Without knowing your situation, I'd recommend splitting the web service into two. This will allow you to have two environments and the two versions of pytorch you need.

Upvotes: 2

Related Questions