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

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',

for lib_path in SALOME_LIBS:

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.


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 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
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__)
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:


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 I should add in the begining of the, the line source $PATH/ ,where $PATH is the path where the file is located?
thank you.

Hello @franco.ota ,

source 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 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 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.


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
#the rest of the python script
import salome

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

Your welcome,

I tried the code below,

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

It didn’t work. Here is the output:

/bin/sh: 1: source: not found

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 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 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/”, line 11, in
mesh =‘C:/S9/’)
File “C:\S9\W64\Python\lib\site-packages\”, line 69, in read
return reader_mapfile_format
File “C:\S9\W64\Python\lib\site-packages\meshio\”, line 36, in read
import h5py
File “C:\S9\W64\Python\lib\site-packages\”, line 33, in
from . import version
File “C:\S9\W64\Python\lib\site-packages\h5py\”, 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
  • on Windows: call env_launch.bat