"ImportError: No module named torch" in ROS package
I've created a ROS package in which there are some python scripts. The python scripts are based on torch module (it is an inference code of pytorch models). when I try to run my scripts it gives me an error:
ImportError: No module named torch 

To install ROS, I used  the instruction of ROS wiki. To validate my installation, I followed the sample code of ROS (a simple publisher and subscriber) and it works well. My sys-info is:


  python: 3.6.9 
  torch: 1.1.0 
  torchvision: 0.3.0 
  OS kernel: Linux 4.15.0-74-generic 
  OS distribution: Ubuntu 18.04.3


I want to import the below libs:

import torch
from cv_bridge import CvBridge
import cv2
import os
import numpy as np
from torch.autograd import Variable
from torchvision import transforms
import torch.nn.functional as F
import torch._utils
import time
from PIL import Image 


My CMake file is as below:

cmake_minimum_required(VERSION 2.8.3)
project(inference_pytorch)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

set(Torch_DIR ".local/lib/python3.6/site-packages/torch/share/cmake/Torch")

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  cv_bridge
)
find_package(Torch REQUIRED)
find_package(OpenCV 3 REQUIRED)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)

###################################
## catkin specific configuration ##
###################################
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES inference_pytorch
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
  ${OPENCV_INCLUDE_DIRS}
  ${Torch_INSTALL_INCLUDE}
  ${Torch_DIR}
)

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

install(PROGRAMS
   scripts/inference_test.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

 catkin_install_python(PROGRAMS scripts/inference_test.py
                      DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

link_directories(
  ${Torch_INSTALL_LIB}
)


Now, how should I edit the CMAKE file to add the mentioned libs to my ROS package?

Finally, I found out that the installed ROS is working with python2 so I installed the torch with pip2. Also, set the torch path in the cmake file. Now it works!
