'How to specify which pytorch to import, if I have two different versions installed?

The PyTorch previously installed in the remote Linux system is problematic (version 1.8.0). It is in the system folders so I don't have privilege to uninstall or upgrade it because I am not a super user. As a result, I installed another PyTorch in my user space using command

pip3 install --user --ignore-installed torch

There are some dependency conflict errors but in the end I was told "Successfully installed torch-1.9.1 typing-extensions-". As shown in the output, the version installed in my user space is 1.9.1. Now, I have two versions of PyTorch installed.

But if I type

import torch

in python3 command line, which version does python3 try to import, the system-wide 1.8.0 version or the version 1.9.1 I newly installed in my user space? How do I specify which version to import? I know I can check the __version__ property, but import crashes in the first place. Thank you.

The environment:

  • A remote Linux with kernel 5.8.0. I am not a super user
  • Python 3.8.6
  • pip 21.2.4

Solution 1:[1]

if you have more than one version of a package installed you can use package resources as follows

import pkg_resources
pkg_resources.require("torch==1.7.1") # The version you want to import 
import torch 



This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 imtiaz ul Hassan