How can I import salome to external python projects in Ubuntu?

There are some basic processes which I do frequently such as making a box and export as stl or reading step files generate surface mesh and export. I was using scripts by running:

salome -t python myscript.py

I want to be able to import salome to my external python projects. How can I do that?

I use Ubuntu 20.04 on wsl in windows11. I’m using linux universal binary Salome-9.9.0.

I assumed I should add some paths and I tried this:

SALOME_LIBS = ['~/Salome/SALOME-9.9.0/BINARIES-CO7/GUI/lib/salome',
               ~/Salome/SALOME-9.9.0/BINARIES-CO7/GEOM/lib/salome',
               '~/Salome/SALOME-9.9.0/BINARIES-CO7/SMESH/lib/salome']

for lib_path in SALOME_LIBS:
    sys.path.append(lib_path)

import salome

But it failed.

Im fairly new to both salome and python. Im sorry if Im asking a trivial question in advance. I appreciate any kind of help.

Umut

Note: I prefer not to use workarounds to automate salome externally such as editting scripts from an external python project and running them in autonomy.

you can try to setup SALOME runtime environment with the env_launch.sh script.
This will ensure that your PYTHONPATH,LD_LIBRARYPATH, etc… environment variables are consistently set and expand yours.

nitawa:/software/SALOME/releases/SALOME-9.10.0> source env_launch.sh
nitawa:/software/SALOME/releases/SALOME-9.10.0> python
import Python 3.6.5 (default, Dec  2 2022, 21:44:27)
[GCC 8.3.1 20190311 (Red Hat 8.3.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import salome
>>> print(salome.__file__)
/software/SALOME/releases/SALOME-9.10.0/BINARIES-CO7/KERNEL/lib/python3.6/site-packages/salome/salome/__init__.py
>>>
1 Like

Thank you so much @NabilG . Terribly sorry for my late response, I was overwhelmed with worked load and didnt see your response earlier. As you suggested, below line solved my problem. :pray:

source env_launch.sh

hello @Umut ,
just to be clear, correct me if i am wrong,
but if I want to run a salome script usign python3 directly, something like, python3 script.py I should add in the begining of the script.py, the line source $PATH/env_launch.sh ,where $PATH is the path where the env_launch.sh file is located?
thank you.

Hello @franco.ota ,

source env_launch.sh is not a python command, you should run it in terminal before you run your python code. After that, your python codes should be able to run import salome. At least this was my case. Check the env_launch.py file briefly, it sets the environment variables required to import salome.

I was able to run my python scripts via python ./myScript instead of salome -t python myscript.py without any change in the script.

Side note: When I try to run the python script from pycharm instead or terminal I failed again, but this is because I made a mess about my python settings in pycharm.

Cheers

1 Like

thanks for the explenation,
I know that is not a python command, thats why i was not understanding. a simple work around would be to use os to run the bash command no?
so adding this to the python script:

import os
os.system("source env_launch.sh")
#the rest of the python script
import salome

this if my brief understanding is correct would make it run correctly using python3 myScript.py
and could also be runned in pycharm or other editors.
no?

Your welcome,

I tried the code below,

import subprocess, os
try:
    subprocess.call('source ~/Salome/SALOME-9.9.0/env_launch.sh',  shell = True)
except:
    os.system("source ~/Salome/SALOME-9.9.0/env_launch.sh")

It didn’t work. Here is the output:

/bin/sh: 1: source: not found
127

For now, I’ve just added the line to my bashrc. Please let me know if you can run the line successfully from the python script.

Terribly sorry. I think I am not getting it correct. I am using Windows and VSCode. I can run the code and it returns the address to the init.py in cmd but not sure how I do the same thing in VSCode.

Dear @NabilG

Sorry to bother you again. I noticed that after I run the source env_launch.sh my python version has changed. Therefore I cant use other packages which are installed in another python version. Is it possible for me to import salome without changing my python version?

I could use help with a similar thing too. I am using Windows and VSCode as I mentioned before. It’s fine for me to use the SALOME python but then I want to change to another python afterwards without relaunching VSCode. Is that possible?

Or how can I install Meshio and Pyvista in SALOME python env? I did try to install them with “python -m pip install [Package]” but they give an error when importing them after importing salome (and only if I use it after salome, it works if I use meshio first). This is the error:

File “c:/PythonAPI/TwoLayerF.py”, line 11, in
mesh = meshio.read(‘C:/S9/Compound_Mesh_3.med’)
File “C:\S9\W64\Python\lib\site-packages\meshio_helpers.py”, line 69, in read
return reader_mapfile_format
File “C:\S9\W64\Python\lib\site-packages\meshio\med_med.py”, line 36, in read
import h5py
File “C:\S9\W64\Python\lib\site-packages\h5py_init_.py”, line 33, in
from . import version
File “C:\S9\W64\Python\lib\site-packages\h5py\version.py”, line 15, in
from . import h5 as _h5
File “h5py\h5.pyx”, line 1, in init h5py.h5
ImportError: DLL load failed: The specified procedure could not be found.

Looks like it fails at “import h5py” but that is successfully installed as well.

OK I got it working. I needed to launch VSCode from the same cmd terminal and then use SALOME interpreter in VSCode.

Hey, i know this is an older thrad, but i am stuck with the same problem. Just a short question: where is the salome interpreter located?

Hi, can you elaborate?
to load salome environment, and be able to do: import salome
in a python session, you need to setup SALOME runtime environment by typing:

  • on Linux : source env_launch.sh
  • on Windows: call env_launch.bat

HTH