Pyzo

Motivation
Pyzo is a free and open-source Python IDE focused on interactivity and introspection, which makes it well-suited for engineering and scientific applications. This page describes how to use it in conjunction with FreeCAD.



Pyzo

 * website: https://pyzo.org
 * GitHub Repository: https://github.com/pyzo/pyzo
 * free and open-source (BSD)
 * fully coded in Python
 * available for Linux, MS Windows and macOS

Versions
Unless otherwise noted, this page assumes the following software versions:
 * Pyzo 4.13.3
 * FreeCAD 0.21.1

Preparing a FreeCAD AppImage in Linux
#!/bin/bash HERE="$(dirname "$(readlink -f "${0}")")" ${HERE}/AppRun python "$@"
 * download the AppImage --> e.g.: FreeCAD_0.21.1-Linux-x86_64.AppImage
 * $ chmod +x ./FreeCAD_0.21.1-Linux-x86_64.AppImage
 * $ ./FreeCAD_0.21.1-Linux-x86_64.AppImage --appimage-extract
 * $ mv ./squashfs-root FreeCAD
 * [some older Linux systems require: export FREECAD_USER_HOME=$HOME ]
 * create a bash script "python_freecad.sh" in ./FreeCAD (in the same directory as bash script "AppRun")

Installing Pyzo

 * use the installer or extract the compressed archive from https://github.com/pyzo/pyzo/releases
 * or install it as a Python module: pip install pyzo (but the version on PyPI is currently not up to date)

Shell Configuration
Start Pyzo and enter the shell configuration dialog via the Menu:

Shell -> Edit shell configurations...

Press the button "Add config" on the top right corner and fill out the form:
 * name
 * freecad (or something else)


 * exe [for Windows]
 * C:\ProgramData\FreeCAD\bin\python.exe


 * exe [for Linux]
 * /home/.../FreeCAD/python_freecad.sh


 * exe [for macOS]
 * /Applications/FreeCAD.app/Contents/Resources/bin/python


 * gui
 * PySide2


 * pythonPath [for Windows and Linux]
 * [leave empty]


 * pythonPath [for macOS]
 * /Applications/FreeCAD.app/Contents/Resources/lib


 * startupScript
 * select radio button "Code to run at startup"
 * enter the following code in the text field:


 * environ:
 * enter the following environment variables:

Finally, press button "Done" in the shell configuration dialog. Run a new "freecad" shell via Pyzo's "Shell" menu. This will start the FreeCAD Gui and a will open a FreeCAD Python shell in Pyzo.

On Linux, when not using an (extracted) AppImage, change  in the text field "startupScript" to e.g. , specifying the directory to the library files "FreeCAD.so" and "FreeCADGui.so". This library directory path could also be added to the field "pythonPath" in the Pyzo shell configuration dialog instead.

The environment variable entry  will tell Pyzo to periodically update FreeCAD's Qt-GUI while FreeCAD is stopped during a breakpoint.

Do not remove the  comment in the startup code -- this is a separator used by Pyzo to split the code into two blocks.

General Pyzo Usage
See https://pyzo.org/guide.html and run the Pyzo Wizard (Menu: Help -> Pyzo Wizard) to get a short introduction.

Noteworthy information:
 * You can enter some "magic" commands in the Pyzo's Python shell, for example "pip install pyerclip". This also works with FreeCAD.
 * Ctrl+I in the shell (or clicking on the flash symbol) will send a KeyboardInterrupt. Ctrl+C is used for the normal copy function.
 * Press F5 or Ctrl+E to execute the whole file. But do not use "Run file as script" as this will restart the shell together with FreeCAD.
 * Press F9 to execute just the selected code.
 * Press Ctrl+Return to execute the current cell.
 * Right click on an editor tab will open a context menu.
 * Many keyboard shortcuts are similar to MATLAB.
 * The "Logger" tool (widget) has a shell to the Pyzo GUI's Python interpreter. You can fully access all objects there, for example copying a shell configuration via.
 * When there is a printed stack trace in Python shell, double click the filename to open the file at the specific line number.
 * When dealing with performance critical code, avoid printing too much to the shell as stream outputs directed to the Pyzo IDE will slow down the execution.
 * There is no  variable defined in the interactive mode, but as a workaround you can run   to get it.
 * In Pyzo's "About" dialog (Help -> About Pyzo) you can see the folder where Pyzo stores the settings.
 * Pyzo can be configured to be portable, e.g to run it from a usb pen-drive with encapsulated settings. To enable this, rename the folder "_settings" in Pyzo's application folder to "settings".
 * You can directly modify Pyzo's source code even in the binary distribution because the binary is just a Python-Interpreter that runs the Python source code in the installation directory.

Example Work Flows
A normal workflow to automate FreeCAD operations is similar to using just FreeCAD. The shell in Pyzo has access to the same Python interpreter as the "Python console" panel in the FreeCAD GUI. Both can be used in a mixed fashion, whatever is more convenient in the situation.

Pyzo brings very nice features to this workflow, just to list some of them:
 * Write a new function in the editor, set a breakpoint and run the code. You can now work inside the function's scope and continue your workflow even manipulating objects manually in FreeCAD.
 * If you started an (almost) endless loop, you can easily interrupt the execution by pressing Ctrl+I in Pyzo or Shell -> Interrupt in the menu.
 * You can have some code fragments prepared in the Pyzo editor tabs and execute individual cells. Try new commands and add them to the code editor to grow your new script.
 * The command history in the Pyzo shell is not cluttered with auto-generated commands but you can still access the automatically inserted comments in the FreeCAD Python panel.
 * If there was an exception which made your code abort execution, just do a post-mortem debug and watch all the variables in different stack layers to find out the cause of the error.

Have a look here and here to see this in action (with an older version where the FreeCAD start was not yet included in the Pyzo shell configuration).

Debugging Start-Up of FreeCAD Modules
To debug the init script of a FreeCAD Mod, e.g. files such as Mod/Draft/Init.py or Mod/Draft/InitGui.py using breakpoints, we need to set a breakpoint in the codefile. These init script files are not directly executed.

During startup, FreeCAD runs the Python scripts src/App/FreeCADInit.py and src/Gui/FreeCADGuiInit.py, and each of these reads the correspondig init scripts (App and Gui) of the Mod and executes them as a string with the code contents:

The interpreter has no more information about the filepath of the executed code. To include the filepath, we need to patch the lines above, as proposed for newer FreeCAD versions.

This is not all. The scripts src/App/FreeCADInit.py and src/Gui/FreeCADGuiInit.py are not individual files anymore but resource files in libraries in the distributed binary versions of FreeCAD. Therefore we will extract them into files and replace the data in the libraries with a short Python script that will call our new out-sourced scripts. So we could also open these new files and set breakpoints there.

The following code will extract the scripts from the libraries, patch them and place the caller code back in the libraries:

We can now, for example, open Mod/Draft/Init.py in Pyzo, set a breakpoint there in line 26 and start the FreeCAD shell in Pyzo. Execution will be interrupted at the breakpoint. We can switch the stack frames, view and manipulate variables and step/continue the code execution.

Known Limitations and Issues
... generally, when importing FreeCAD as a module in Python:
 * auto backups are not enabled --> save often
 * custom FreeCAD Qt-GUI color themes are not available

Feedback
To ask questions about this topic, share ideas, give input, point out mistakes, etc, please write a message to the initial topic in the FreeCAD forum or create a new one.

Miscellaneous Links

 * Official Pyzo Website
 * Pyzo GitHub Repository
 * initial topic in the FreeCAD forum
 * Python_Development_Environment
 * Debugging
 * Embedding_FreeCAD