Advice requested on developing python workbench in VS Code
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Advice requested on developing python workbench in VS Code
Hello all,
I am starting to experiment with developing a workbench in python using VS Code. I am a retired programmer, relearning Python after many years away. From my reading VS code seems to be the preferred environment on Windows. I am basing my project on the Workbench Starter Kit by Joel Graff. I would appreciate any suggestions about how to develop in this environment.
For example, I can start Freecad from the terminal but my breakpoints are not being triggered. I hope that once I can get my workflow functional that I can concentrate on the algorithms, etc.
Thanks in advance,
Frank Alviani
I am starting to experiment with developing a workbench in python using VS Code. I am a retired programmer, relearning Python after many years away. From my reading VS code seems to be the preferred environment on Windows. I am basing my project on the Workbench Starter Kit by Joel Graff. I would appreciate any suggestions about how to develop in this environment.
For example, I can start Freecad from the terminal but my breakpoints are not being triggered. I hope that once I can get my workflow functional that I can concentrate on the algorithms, etc.
Thanks in advance,
Frank Alviani
Re: Advice requested on developing python workbench in VS Code
You need to add some code to your python file that activates ptvsd.
See: https://wiki.freecadweb.org/Debugging
Expand the section "Visual Studio Code (VS Code)" for details.
I add the following to the top of my python:
See: https://wiki.freecadweb.org/Debugging
Expand the section "Visual Studio Code (VS Code)" for details.
I add the following to the top of my python:
Code: Select all
DEBUG = True
if DEBUG:
import ptvsd
print("Waiting for debugger attach")
# 5678 is the default attach port in the VS Code debug configurations
#ptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.wait_for_attach()
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: Advice requested on developing python workbench in VS Code
Thanks for the additional info. As far as I can tell, your instructions are for somebody working on a macro. I've installed ptvsd as directed and put in the debug configuration (the version of VS Code I'm working with simpilfies that, by the way).
I'm trying to create a workbench, however, and I'd like to have FreeCad find it like any other installed workbench; this should allow me, in theory, to put a breakpoint in the __init__.py file to stop FreeCad when it starts loading my code, then I can put in additional breakpoints as needed.
I guess my first stumbling block is how to have FreeCad find my folder for launching, so I can proceed. Do I need to put it in the folder? If so, are there any git tricks to relocating it?
Thanks for the help - this is a lot to learn...
-Frank Alviani
I'm trying to create a workbench, however, and I'd like to have FreeCad find it like any other installed workbench; this should allow me, in theory, to put a breakpoint in the __init__.py file to stop FreeCad when it starts loading my code, then I can put in additional breakpoints as needed.
I guess my first stumbling block is how to have FreeCad find my folder for launching, so I can proceed. Do I need to put it in the
Code: Select all
C:\Users\frank\AppData\Roaming\FreeCAD\Mod
Thanks for the help - this is a lot to learn...
-Frank Alviani
Re: Advice requested on developing python workbench in VS Code
Hi,
I have the same question (how to get the debugger to 'see' custom workbench code).
I am using debugpy, which is set up in the following way:
1. Create init.py and initGui.py in a new folder under Mod (e.g. C:\Program Files\FreeCAD\Mod\MyWorkbench) (1), following the templates in the Workbench Creation wiki.
2. Install debugpy in the embedded python distribution,
e.g. As administrator, give the command
3. Configure debugpy by placing a snippet such as below into init.py
e.g.
4. Open the FreeCAD install directory (e.g. C:\Program Files\FreeCAD\) in VSCode
5. Create a launch configuration to connect to FreeCAD
e.g.
6. Start FreeCAD and launch/connect from VSCode.
At this point, VSCode connects OK, and can break into existing python code.
For example, giving the command breakpoint() in the Python Console in FreeCAD, then interacting with the GUI in some way, will break into Python in VSCode.
However, if I include the command breakpoint() in a custom command, or place a breakpoint in VSCode in the custom workbench, it won't.
For example, in initGui.py, create and register a command,
Execute My_Command in FreeCAD,
e.g. in the Python Console,
The console will show the result of the print statement ("I am executing some stuff here!"), but the debugger won't do anything.
I can only guess that during the build that something happens to the embedded workbench source, which means its treated differently by the debugger, but I have no idea what that is.
(1) This is so the VSCode working directory can be set as C:\Program Files\FreeCAD\, allowing it to the existing workbench source, as well as custom workbench source.
I have the same question (how to get the debugger to 'see' custom workbench code).
I am using debugpy, which is set up in the following way:
1. Create init.py and initGui.py in a new folder under Mod (e.g. C:\Program Files\FreeCAD\Mod\MyWorkbench) (1), following the templates in the Workbench Creation wiki.
2. Install debugpy in the embedded python distribution,
e.g. As administrator, give the command
Code: Select all
C:\Program Files\FreeCAD\bin>python -m pip install debugpy
e.g.
Code: Select all
import debugpy
debugpy.configure(python="python")
debugpy.listen(5678)
debugpy.trace_this_thread(True)
debugpy.debug_this_thread()
5. Create a launch configuration to connect to FreeCAD
e.g.
Code: Select all
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"justMyCode": false,
"redirectOutput": true,
"logToFile": true
}
]
}
At this point, VSCode connects OK, and can break into existing python code.
For example, giving the command breakpoint() in the Python Console in FreeCAD, then interacting with the GUI in some way, will break into Python in VSCode.
However, if I include the command breakpoint() in a custom command, or place a breakpoint in VSCode in the custom workbench, it won't.
For example, in initGui.py, create and register a command,
Code: Select all
class My_Command_Class():
"""My new command"""
def GetResources(self):
return {'Pixmap' : 'My_Command_Icon', # the name of a svg file available in the resources
#'Accel' : "Shift+S", # a default shortcut (optional)
'MenuText': "My New Command",
'ToolTip' : "What my new command does"}
def Activated(self):
"""Do something here"""
breakpoint()
print("I am executing some stuff here!")
return
def IsActive(self):
"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
return True
FreeCADGui.addCommand('My_Command',My_Command_Class())
e.g. in the Python Console,
Code: Select all
FreeCADGui.runCommand("My_Command")
I can only guess that during the build that something happens to the embedded workbench source, which means its treated differently by the debugger, but I have no idea what that is.
(1) This is so the VSCode working directory can be set as C:\Program Files\FreeCAD\, allowing it to the existing workbench source, as well as custom workbench source.
Re: Advice requested on developing python workbench in VS Code
Following up the above, I have found that if a command is in a separate file, the debugger works OK.
To do this, define the command in a file, e.g. commands.py, next to the Init.py & InitGui.py files, and import this in InitGui.py,
e.g.
When executing the command from the Python Console in FreeCAD (e.g. with "FreeCADGui.runCommand("My_Command")") the debugger breaks into commands.py successfully, whether using breakpoint() or placing a breakpoint in VSCode.
Additionally, the commands.py file can be re-loaded with execfile(),
e.g.
In the Python Console in FreeCAD. (FreeCADGui.addCommand seems fine with being called multiple times.)
This allows changing the implementation without restarting FreeCAD.
(The FreeCAD Command wiki implies that commands should be placed in separate files in any case.)
To do this, define the command in a file, e.g. commands.py, next to the Init.py & InitGui.py files, and import this in InitGui.py,
e.g.
Code: Select all
import commands
Additionally, the commands.py file can be re-loaded with execfile(),
e.g.
Code: Select all
execfile("C:\Program Files\FreeCAD\Mod\Electrical\commands.py")
This allows changing the implementation without restarting FreeCAD.
(The FreeCAD Command wiki implies that commands should be placed in separate files in any case.)
Re: Advice requested on developing python workbench in VS Code
Hi Sebjf,
I tried to follow your directions. I installed debugpy from the bin directory of my executable Freecad via the regular command window. Should I have done it from the vscode terminal?
But when I launch Freecad and then "debug" from the VS debugger, I just get:
even though the freecad directory I'm using and executing from is at "E:\Dev\FreeCAD19_Output"
I'm afraid your directions about setting up the configurations were a little ambiguous. When you say "Create a launch configuration to connect to FreeCAD" do you mean directly in the top directory? When I created one VSCode insisted on putting it into the (newly created) .vscode directory.
Thanks in advance for any help,
Frank
I tried to follow your directions. I installed debugpy from the bin directory of my executable Freecad via the regular command window. Should I have done it from the vscode terminal?
But when I launch Freecad and then "debug" from the VS debugger, I just get:
Code: Select all
File "C:\Users\frank\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 239, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
ValueError: source code string cannot contain null bytes
I'm afraid your directions about setting up the configurations were a little ambiguous. When you say "Create a launch configuration to connect to FreeCAD" do you mean directly in the top directory? When I created one VSCode insisted on putting it into the (newly created) .vscode directory.
Thanks in advance for any help,
Frank
- ebrahim raeyat
- Posts: 625
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
Re: Advice requested on developing python workbench in VS Code
I have been using VSCode and it is fantastic. I can debug my python files without any effort. example is my civil WB, I will push some more files soon:
https://github.com/ebrahimraeyat/Civil/ ... safe/punch
you can read report.py and test\test_report.py
for windows you can use this lines at the begining of file:
https://github.com/ebrahimraeyat/Civil/ ... safe/punch
you can read report.py and test\test_report.py
for windows you can use this lines at the begining of file:
Code: Select all
FREECADPATH = 'G:\\program files\\FreeCAD 0.19\\bin'
sys.path.append(FREECADPATH)
import FreeCAD