vscode setup for python module import to work
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
vscode setup for python module import to work
Edit: this thread was split from here since it really discusses another vscode setup detail:
https://forum.freecadweb.org/viewtopic.php?f=22&t=40673
I am now using vscode to develop python and while I can see variables during debug, when just editing, it gives lots of errors about un-found modules and such.
I'll have a look at the set up described in this project, but the first glance appears the instructions of what to do in vscode are too cursory and assume a great deal of understanding of vscode configuration concepts.
I hesitate to use this as I see the paths are shown as 0.19 and I'm using 0.20.
In addition, I find vscode has SO many setting options (half the time it gets the indent tab/space conversion wrong...and I've scoured the web and messed with env file forever.) And I found myself in settings hell so many times I'm afraid to change things once things are near what works.
https://forum.freecadweb.org/viewtopic.php?f=22&t=40673
I am now using vscode to develop python and while I can see variables during debug, when just editing, it gives lots of errors about un-found modules and such.
I'll have a look at the set up described in this project, but the first glance appears the instructions of what to do in vscode are too cursory and assume a great deal of understanding of vscode configuration concepts.
I hesitate to use this as I see the paths are shown as 0.19 and I'm using 0.20.
In addition, I find vscode has SO many setting options (half the time it gets the indent tab/space conversion wrong...and I've scoured the web and messed with env file forever.) And I found myself in settings hell so many times I'm afraid to change things once things are near what works.
Last edited by drmacro on Wed May 05, 2021 10:49 pm, edited 1 time in total.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
I have the following in .env:
And the following in settings.json
And all imports in my code have this error:
Any ideas what I'm doing wrong?
Code: Select all
FREECAD_LIB=/home/mac/freecad-build/lib
FREECAD_MOD=/home/mac/freecad-build/Mod
PYTHONPATH=${FREECAD_Mod}:${FREECAD_LIB}:${PYTHONPATH}
Code: Select all
{
//"python.pythonPath": "/usr/bin/python"
"python.envFile": "${workspaceFolder}/.env"
}
Code: Select all
Unable to import 'FreeCAD'
Unable to import 'FreeCADGui'
...
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
Would it be difficult to make this work with Atom on Windows? Also just a question, whenever I change the code of a macro I have to restart FreeCAD to make it work properly. Is there any way in which I would not have to do it?
-
- Veteran
- Posts: 3192
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
You can reload files with importlib.reload() but it can be tricky:
https://forum.freecadweb.org/viewtopic. ... 24#p405128
https://forum.freecadweb.org/viewtopic. ... 24#p405128
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
It is always tricky I think I will stick with copy/paste for now until I understand things better
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
the ptvsd connection works fine and I can debug, single step, explore variables, etc.
it's editing code with autocomplete that is the issue, that and always having 64 errors because vscode can't find the FreeCAD modules.
I am running from source, so I don't have anything in /usr/lib/...
I think I knew where the actual module that 'import FreeCAD' lives I could fix this. But I can only find FreeCAD.so, not FreeCAD. py.
it's editing code with autocomplete that is the issue, that and always having 64 errors because vscode can't find the FreeCAD modules.
I am running from source, so I don't have anything in /usr/lib/...
I think I knew where the actual module that 'import FreeCAD' lives I could fix this. But I can only find FreeCAD.so, not FreeCAD. py.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
So for what it's worth, I am am completely new in FreeCAD development. I am also new to Python too but I have done MVC web development and plenty of C/C++/shell script Unix work and spent years as an SAP developer so I am not brand new and more importantly I have this working both ways in FreeCAD. ( all I have to do is now learn some python and how FreeCAD is coded )drmacro wrote: ↑Tue May 04, 2021 12:34 pm I have the following in .env:
And the following in settings.jsonCode: Select all
FREECAD_LIB=/home/mac/freecad-build/lib FREECAD_MOD=/home/mac/freecad-build/Mod PYTHONPATH=${FREECAD_Mod}:${FREECAD_LIB}:${PYTHONPATH}
And all imports in my code have this error:Code: Select all
{ //"python.pythonPath": "/usr/bin/python" "python.envFile": "${workspaceFolder}/.env" }
Any ideas what I'm doing wrong?Code: Select all
Unable to import 'FreeCAD' Unable to import 'FreeCADGui' ...
This is perhaps a bit long but I personally don't find instructions that presume I can connect undocumented esoteric dots that helpful.
=====
I guess it depends what your trying to do and how your trying to do it. What I demonstrated in the video was how to take an existing body of code, execute that code in FreeCAD and attach to it with the VS Code IDE debugger. The example was simple but this afternoon I was using that same procedure to try and investigate a problem in another work bench. I stuck the 3 lines of code below into one of the files,
Code: Select all
import ptvsd
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.break_into_debugger()
If you develop in that manner and start the code in FreeCAD then your letting FreeCAD bootstrap the program and find the libraries.
On the .env file...
You want to check if the files and libraries you are looking for are where you think they are. If not add the directories as lib paths. This is not always straight forward. There are different ways to include files and that determines what the path must be. More below.
This is my .env file.
Code: Select all
FREECAD_BIN=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/bin
FREECAD_LIB=C:/Colin/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/lib
FREECAD_MOD=C:/Colin/Downloads/FreeCAD-0.19.1.a88db11/Config/AppData/Roaming/FreeCAD/Mod
FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/
PYTHONPATH=${FREECAD_BIN};${FREECAD_LIB};${FREECAD_MOD};${FREECAD_EXT}
Within FreeCAD this line works.
Code: Select all
from PySide import QtCore, QtGui
Code: Select all
FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/
If you wanted to put in
Code: Select all
import QtCore
Import QtGui
Code: Select all
FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/PySide
Code: Select all
{
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"redirectOutput": true,
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "c:/temp"
}
]
}
]
}
This will run the debugger using the current file.
This will run the debugger using remote attach mode
Hope this helps. Feel free to PM me if you need more details.
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
Your FreeCAD.so file is dll file made from a python module on a Linux system. In Windows this is the FreeCAD.pyd file. That is in essence your FreeCAD.py file. In your .env file enter the individual paths in separate lines( just like my previous post) where ever a .so file or a .py file is found. String them all together like:drmacro wrote: ↑Tue May 04, 2021 11:33 pm the ptvsd connection works fine and I can debug, single step, explore variables, etc.
it's editing code with autocomplete that is the issue, that and always having 64 errors because vscode can't find the FreeCAD modules.
I am running from source, so I don't have anything in /usr/lib/...
I think I knew where the actual module that 'import FreeCAD' lives I could fix this. But I can only find FreeCAD.so, not FreeCAD. py.
Code: Select all
PYTHONPATH=${FREECAD_BIN}:${FREECAD_LIB}:${FREECAD_MOD}:${FREECAD_EXT}
Try that.
Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD
From my home directory, find .so files:
Code: Select all
find . -iname '*.so' 2>/dev/null | grep freecad-
./freecad-build/Mod/Draft/DraftVecUtils.py
...
./freecad-build/lib/FreeCAD.so
...
Code: Select all
{
//"python.pythonPath": "/usr/bin/python"
"python.envFile": "${workspaceFolder}/.env"
}
Code: Select all
FREECAD_LIB=/home/mac/freecad-build/lib/
FREECAD_MOD=/home/mac/freecad-build/Mod/
PYTHONPATH=${FREECAD_MOD}:${FREECAD_LIB}:${PYTHONPATH}
vscode:
Code: Select all
import FreeCAD
...
import DraftVecUtils
Unable to import 'DraftVecUtils'
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: vscode setup for python module import to work
This topic was split on drmacros suggestion from https://forum.freecadweb.org/viewtopic. ... 20#p501571.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.