2

how to import a torch 1.7.1 when torch 1.4.0 is also installed

When I run the command: ! pip list It lists all libraries with : torch 1.7.1

Now when I run:

>>>import torch
>>>torch.__version__
'1.4.0'

How Do I import torch==1.7.1 in the python program? I am using python 3.8.3 and windows 10

  • Does this answer your question? [Force python to use an older version of module (than what I have installed now)](https://stackoverflow.com/questions/6445167/force-python-to-use-an-older-version-of-module-than-what-i-have-installed-now) – ndrwnaguib Mar 10 '21 at 12:59

2 Answers2

1

Slightly different way to answer your question, but if you want to have two versions of torch installed simultaneously for different purposes (e.g. running different programs), my recommendation would be to use torch 1.7.1 and torch 1.4.1 in separate virtual environments. Please see the links below for guides on getting started with these:

  1. virtualenv:
# Create virtual environment
python3 -m venv torch-env

# Activate virtual environment
torch-env\Scripts\activate.bat

# Any packages you install now are ONLY for this virtual environment
pip install torch==1.7.1  # Or 1.4.1 in a separate environment
  1. conda:
# Create virtual environment with Python 3.8.3
conda create --name torch-env python=3.8.3

# Activate virtual environment
conda activate torch-env

# Install torch
pip install torch==1.7.1  # Or 1.4.1

Creating separate virtual environments will allow you to have multiple versions of torch simultaneously without any issues. You should then be able to import as you do above and use them interchangeably simply by switching environments.

Ryan Sander
  • 419
  • 3
  • 6
0

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")
import torch 

reference

imtiaz ul Hassan
  • 358
  • 3
  • 14