Using autcompletion with Visual studio code
This forum is DEPRECATED, please create new topics in the new SALOME forum.
For existing topics please transfer them to the new forum.
Is it possible to add autcompletion feature while writing Salome scripts in Visual Studio Code?
I tried to add/home/s1291//Salome_9.2_UBUNTU1804/SALOME-9.2.0-UB18.04-SRC/BINARIES-UB18.04/KERNEL/lib/python3.5/site-packages/salome
to PYTHONPATH
environment variable in ~/.bashrc
file but there is no autocompletion!
Any idea?
Hi,
you need to set the environment beforehand.
- Linux:
- run source ./env_launch.sh and start VS ( code)
- Windows:
- start a VS dev terminal
- in that terminal:
- cd the installation folder
- type: call <SALOME installation folder>work\set_env.bat
- type: call <SALOME installation folder>\products\env_launch.bat
- start visual studio with devenv.exe
- autocompletion should work
Thank you that works (by choosing python version used by salome 9.2: 3.5.1 inbVScode)
thanemireth dear Nabil
Thank you in advance for your time.
I am attempting to write plugins for Salome Shaper that will create threads and other topologies to make modeling for 3D printing easier. Having autocompletion is how I learn a new programming environment.
What I have done:
I am on Windows 10, I have downloaded and installed Salome 9.5.0 in a path with a short name and no special characters (c:\dev\apps\Salome_9_5_0\... )
I have downloaded and installed MS Visual Studio Code, version 1.51.1, and then installed both the Microsoft Python Interpreter extension and pylint.
I have launched Salome, and run Tools>Plugins>Launch VS Code (debugger). This launches Visual Studio Code successfully.
From within VS Code, I opened a Terminal window, and tried to type the above commands (call c:\dev\apps\Salome_9_5_0\work\set_env.bat ... (see problem below)
I also attempted to set the paths inside the VS Code application, under the Python interpreter settings @ext:ms-python.python; setting python.autoComplete.ExtraPaths .
However, I have the following problems:
1) the VS Terminal does not recognize the "call" command, but send the message: "the Term 'call' is not recognized ....
2) I get pylint errors for all imports of any module as follows: "No name 'geom' in module 'salome' pylint(no-name-in-moduel) [30,5]" , etc.
3) I cannot find any set_env.bat file in any of the Salome installation directories. (Is this because I did not cmake my installation, but instead installed from the Salome-9.5.0.exe file?)
Please help direct me to enable autocompletion in some sort of python development environmont.
(I'm more competent than these questions might appear, but self taught on both Salome and python.)
Thank you again for your time.
Vector H.
Thank you for your time.
I believe I have solved my problem.
On Windows, with a .py script open in Visual Studio Code, choose Terminal > New
(This opens a powershell window)
In the powershell window, type:
cd <SalomeRootDir> (enter)
start env_launch.bat (enter)
where <SalomeRootDir> is the the directory on windows that contains the W64 directory, for example c:\Salome\Salome-9.5.0
Now autocompletion will work.
Note that there are three changes from the expert post above:
a) the powershell command is "start" not "call"
b) the batch file set_env no longer seems to exist, so one simply runs the env_launch.bat file
c) the env_launch.bat file only exists in the installation directory, not in each product directory (eg GEOM, SHAPER, etc.)
I hope this is helpful.
PS, to switch the Terminal window to classic command syntax, one may type cmd and press enter, to swich it back to powershell mode, type powershell and press enter.
This is still not fully functional.
I have also discovered that the supplied env_launch.bat file seems to have a bug in it: namely the out_dir_Path variable includes a trailing backslash, but it is everywhere in the batch file appended with another backslash before the W64 directory name.
I changed the code to the following:
set out_dir_PathFull=%~dp0
set out_dir_Path=%out_dir_PathFull:~0,-1%
that strips off the final character (the offending extra backslash).
Sadly, even with this modification, I still have an abundance of errors in Visual Studio Code... namely it cannot find salome.geom.
"No name 'geom' in module 'salome'
The import salome is now resolving (inside Visual Studio Code) to .../W64/KERNEL/lib/python3.6/site-packages/salme/salome/__init__.py.
I understand from the documentation in that file that we are expecting the paths to be appended such that whenever there is a <MODULE_ROOT_DIR>/lib/pythonX.Y/site-packages/salome/<ROOT_PYTHONPACKAGE_NAME> then those python files will be appended into a large "virtual python package."
This clearly works inside the Salome project itself, however it is not working in VS Code. (It could be that VS Code simply cannot resolve all those vitual python packages, but only stores the first one it encounters)
I'm guessing from Nabil's earlier post that each module subdirectory was one time the home for an env_launch.bat file, in which case his instruction might have made sense to point the paths to the __init__.py needed by the module one is working on. However, since there now seems to be only one env_launch.bat (and no set_env.bat), this method won't work
I hope that's clear and my sincerity in seeking an answer comes through.
Again, we are looking for a way to use VS Code to have auto-completion and error checking while working on Salome plugin scripts, but the environment setup is not allowing us to find a library that will do both "import salome" and "import geom" and "from salome.geom import geomBuilder"
Thank you for your time.
Hi Vector,
apologize for the late reply. The prescription I wrote was aimed to provide autocompletion within Visual studio in a visual studio console for SALOME 9.3.0
In case of SALOME 9.5.0, in order to develop and debug a script, here is how I proceed:
- start SALOME
- In the main bar menu, select tools -> Plugins -> Launch VS Code (debugger), assuming that you extended your PATH environment variable with the VSCode installation directory
- edit your script
- click on Run - Start debugging
- as Debug configuration, select Python File
- Define your breakpoints, etc..
you don't need to source environment, since this is already set within SALOME.
See enclosed screen capture, which shows a sample session.
Auto completion works as well, given that you start VSCode from SALOME.
Thank you for your attention!
And I apologize again for my not being able to make it work.
I am able to proceed as you described this far:
Start Salome - check
In the main bar, select Tools > Plugins > Launch VS Code (debugger) – check
VS Code launches in a separate window
Edit my script – check, for this example I’m still trying to open salome_plugins.py from the …W64\share\salome\plugins\gui\demo folder.
Click on Run – Start debugging – check
As Debug Configuration, select Python file – check.
xxx
Now my screen shows as follows: it will not run with these errors. There is still something fundamentally wrong with my environment.
Auto-completion also does not work.
It appears to me that there is some installation/installation step that’s not the same in our environments, but I am so far at a loss to figure out what it is.
Thank you again for your help.
In looking at this further, I think the following information and questions might help:
Regarding PATHs:
In
Salome, in the Python Console, if I type;
import sys print (sys.path)
print (sys.path)
then I get a long system path which include many of the form:
<installationdirectory>\\W64\\<module
name>\\bin\\salome and
<installationdirectory>\\W64\\<module
name>\\lib\\salome
<installationdirectory>\\W64\\<module
name>\\lib\\python3.6\\site-packages listed.
However,
after I launch VS Code from Salome, and go to the terminal window, and type:
$env:path
then I also get a long list of paths, but they are not the same at all.
Notably, in VS Code the <installationdirectory>\W64\<module name>\lib\python3.6\site-packages
are missing.
I tried to add these missing paths to the path within VS Code using $env:path = ($env:path + $astringwiththemissingpaths), but that did not solve my problem.
I also tried adding them to the json.settings file (see below) but I could still not autocomplete. Thoughts?
Questions:
1)
In Visual Studio Code, I was prompted to install a Python extension... I used
ms-python.python v2020.11.371526539.
Are you using the same?
2)
if you're also using that python extension, then from the Extensions panel, if
you click the gear box, it will take you to a settings editor headed by
@ext:ms-python.python.... searching this for autocomplete turns up a setting
called Python > Auto Complete: Extra Paths, where you may make changes by
clicking 'edit in settings.json'
Do you have any paths setup there? If so, could you copy
them here?
3)
There is another setting called Linting: Ignore Patterns, which says it is “Patterns
used to exclude files or folders from being linted” which as by default the
pattern “**/site-packages/**/*.py
Do you have this setting enabled with
this pattern? (I removed it, but no luck)
4)
Similar to (3), there’s a setting “Workspace Symbols: Exclusion Patterns, which
also defaults to **/site-packages/**
Do you have this setting enabled? (I
also removed this, but still no luck, I set them both back again)
I found it!!
In VS Code, go to Settings by pressing Ctrl + ,
Search for python.languageserver, change from Jedi to Pylance. Download and set this as your default.
Reload, and the autocompletion and resolution to definitions works.
Notably, under the Pylance settings, it automatically reads the Salome installation and detects the additional directories (ending in .../site-packages/salome) and adds them to its parameter Python > Analysis: Extra Paths.
I found this solution on this board: https://stackoverflow.com/questions/64255834/no-definition-found-for-function-vscode-python
Thank you, thank you for your help!
Just to beat this to death, I need to correct a statement in the previous post: Pylance does not automatically read the Salome installation for the paths and apply them to Python > Analysis: Extra Paths... in my case this was left over from my earlier tinkering.
In fact, it seems to me that Pylance resolves the Salome python path structure with no need for "Extra Paths" at all (unless you want it to resolve tubebuilder and tubedialog, in which case you need to add the path to the gui demo folder eg: <installation-dir>\W64\GUI\share\salome\plugins\gui\demo )
Thanks again!
This forum is DEPRECATED, please create new topics in the new SALOME forum.
For existing topics please transfer them to the new forum.