Path setting for Python models

Hi to all Persalysers,

As indicated in the title, with @josephmure we have created a Python model, which needs to import an external Python library. To do this, we had to set manually the path in the Python script defining the model, by adding the following lines:

import sys
sys.path.append("/home/H01971/MAGIC/incendie-inversion/wrapper")
import persalys_otmagic as otm

Hence, anyone else wanting to reproduce our study will have to manually change the path inside Persalys Python model window. We would like to know if there exist more intelligent / automatic way to set paths for Persalys.

Many thanks in advance for your response!

Best
Merlin

1 Like

Hello Merlin,

That would indeed be the way to go, at least with the linux AppImage. You could also run pip in perslays python console to install a module directly from a repository. For example:

import pip
pip.main(["install", "salome-med"])
import med

The windows approach is simpler though. You can directly paste your module in persalys install directory’s site-package

Cheers,
Guillaume

1 Like

you can also install your module in your user site (~/.local), that way you dont even need to call sys.path

Hi, @mbaudin47 showed me the following alternative.

Under Linux, you can set an environment variable in your .bashrc file :

export MAGIC_PATH="$HOME/librairies/magic" 

Then, provided Persalys was started using a terminal console, you can access this environment variable within Persalys:

>>> import os
>>> print(os.environ["MAGIC_PATH"])
1 Like