Import a custom python library

Typically: "How do I... ", "How can I... " questions
Post Reply
mhr_azizi
Posts: 21
Joined: 18 Jan 2022, 16:25

Import a custom python library

Post by mhr_azizi »

Hello,
I have developed a Python library consists of some functions and classes and want to import it in a Python Child Script in V-REP. Could you please help me how can I import it? I have copied the .py associated file in the current directory of my V-REP file, but the module could not be imported.
Regards,

coppelia
Site Admin
Posts: 10379
Joined: 14 Dec 2012, 00:25

Re: Import a custom python library

Post by coppelia »

Hello,

can you tell me the error message you are seeing?
Just for info, we will release a new CoppeliaSim version (V4.6) in less than 2 weeks, and Python support will be greatly improved.

Cheers

mhr_azizi
Posts: 21
Joined: 18 Jan 2022, 16:25

Re: Import a custom python library

Post by mhr_azizi »

Thank you so much for your response. The module I have developed is named Inteligent_Controller, and when I import it in the child script, the error message " No module named 'Inteligent_Controller' " is shown.
regards,

coppelia
Site Admin
Posts: 10379
Joined: 14 Dec 2012, 00:25

Re: Import a custom python library

Post by coppelia »

Please show me your child script code

mhr_azizi
Posts: 21
Joined: 18 Jan 2022, 16:25

Re: Import a custom python library

Post by mhr_azizi »

Code: Select all

import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import torch.optim as optim
import random 
import torch.utils.data as data
from collections import deque as dq
import copy
import os
import Inteligent_Controller 

class Environment():
    
    def __init__(self, Xc_min, Yc_min, delta_Xc, delta_Yc, max_episod_time_stem):
        self.Xc_min = Xc_min
        self.Yc_min = Yc_min
        self.delta_Xc = delta_Xc
        self.delta_Yc = delta_Yc
        self.Xc_max = self.Xc_min + self.delta_Xc
        self.Yc_max = self.Yc_min + self.delta_Yc
        self.delta_Phi = 2 * np.pi
        self.state = [0., 0., 0., 0., 0., 0.]
        self.fail = False
        self.done = False
        self.Zc = 0
        self.alpha = 0
        self.beta = 0
        self.done_timestep_record = 0
        


This is a part of the code I have developed, and the error is associated with line 11 ("import Inteligent_Controller").
Inteligent_Controller is the name of a python module consists of some classes and functions that I have developed, and its .py file is in the directory of .ttt file.

Regards,

fferri
Posts: 1239
Joined: 09 Sep 2013, 19:28

Re: Import a custom python library

Post by fferri »

Hello,

the scene path is not automatically added to the package search path (only the 'python' directory of CoppeliaSim is).

You can add your scene path to Python's package search path with:

Code: Select all

import sys
scenePath = sim.getStringParam(sim.stringparam_scene_path)
sys.path.append(scenePath)
# import Intelligent_Controller

mhr_azizi
Posts: 21
Joined: 18 Jan 2022, 16:25

Re: Import a custom python library

Post by mhr_azizi »

It works properly.
Many thanks Ferri.

Post Reply