Rishabh Chaurasia
Rishabh Chaurasia

Reputation: 21

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

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

Upvotes: 2

Views: 1157

Answers (2)

imtiaz ul Hassan
imtiaz ul Hassan

Reputation: 388

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

Upvotes: 0

Ryan S
Ryan S

Reputation: 479

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.

Upvotes: 1

Related Questions