Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
bdm
Posts: 178
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

jonasb wrote: Sun May 07, 2023 9:04 pm This made the message no away on my Mac. See also my post from May 5th, above.
Nice solution, @jonasb.

A small hint for users of the Linux AppImage:
I had to change "PySide" to "PySide2" and then it also works in Linux.
And also note, that in the Pyzo shell configuration dialog, radio button "Code to run at startup" has to be selected first before entering the code.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

Thanks @jonasb and @bdm, no message now :D
JorgeFernandes
Posts: 74
Joined: Sun May 07, 2023 12:18 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by JorgeFernandes »

This really helps me.
I'm using Ubuntu, so the freecad was already installed in the system and my lack of knowledge makes me spend some time because some commands work and others do not.

But I download the AppImage you mention, and configure vscode, using a similar approach as yours and everything is working now. I can reproduce the commands from freecad console with Python script. I even got some new prints, that I believe come from the new version of freecad :lol:

I have one question though, how do you use commands that have:

Code: Select all

import ImportGui
I wasn't able to reproduce it because always gives the error:

Code: Select all

Cannot load Gui module in console application.
Thanks for all the help.
bdm
Posts: 178
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

JorgeFernandes wrote: Sun May 28, 2023 10:39 am
Hi Jorge,

This is a small example for doing "import ImportGui" without Pyzo on Linux.
Create a file "/tmp/example.py" with this code

Code: Select all

import os
import sys

if sys.platform == 'linux':
    sys.path.append(os.environ['PATH_TO_FREECAD_LIBDIR'])

import FreeCAD
import FreeCADGui

FreeCADGui.showMainWindow()

# if you do not want the window open, then hide it
FreeCADGui.getMainWindow().hide()

import ImportGui

print('finished')
And then run "./python_freecad.sh /tmp/example.py" in a terminal.
How to make "python_freecad.sh" is described in my first post in this thread.
jonasthomas
Posts: 162
Joined: Wed Feb 01, 2012 3:29 am

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by jonasthomas »

@bdm
Thank you for posting this. You're method using pyzo is working for me.
jonasthomas
Posts: 162
Joined: Wed Feb 01, 2012 3:29 am

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by jonasthomas »

If someone is going down this route using Pyzo, I found this link helpful:
http://www.cs.toronto.edu/~guerzhoy/c4m ... PartA.html
In particular, I wasn't sure how to look at variable values using Pyco
There is a youtube link that explains it.
Also, there is a link to python workshops
http://www.cs.toronto.edu/~guerzhoy/c4m ... e_archive/
Looked sort of interesting.. (I'm more comfortable with C#,C++ then I am with Python.
bdm
Posts: 178
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

jonasthomas wrote: Thu Jul 13, 2023 5:39 pm Thank you for posting this. You're method using pyzo is working for me.
Thanks for the nice feedback.
jonasthomas wrote: Fri Jul 14, 2023 4:46 am In particular, I wasn't sure how to look at variable values using Pyzo
I mostly type the expressions in the shell to see the value. If you select an expression in the code editor (not more than one line) and press F9, the expression is evaluated and the result printed to the shell. The expression will then also be copied to the shell's command history. To quickly select a word (eg. variable name), just double click it. The "Interactive help" widget will also show the value (e.g. during moving in the autocomplete list). There is a special case when executing whole cells: the representation of the last expression will be printed to the shell.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

Hello, when a .py file containing the command print(__file__) is run in Pyzo, it throws the following error: NameError: name '__file__' is not defined. It behaves as though you were executing it directly within a Python interpreter. The correct output should be the complete path to the file: C:\Users\user\AppData\Roaming\FreeCAD\Macro\test.py.
Is there any way to fix this issue?
bdm
Posts: 178
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

heron wrote: Mon Jul 24, 2023 6:01 pm Hello, when a .py file containing the command print(__file__) is run in Pyzo, it throws the following error: NameError: name '__file__' is not defined.
Hi @heron,

this is normal. If you want the classic behaviour with a defined __file__, you can select "Run -> Run file as script" (or Ctrl+Shift+E), but this will also restart the shell.

As I mostly work interactively, I prefer to have the following lines on top of the script if I need the path of the current script that contains these lines. This works for all cases, even if you just run selected code or a cell.

Code: Select all

import inspect
__file__ = inspect.getfile(inspect.currentframe())
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

Hello @bdm and many thanks for the quick and accurate reply.
bdm wrote: Mon Jul 24, 2023 6:11 pm you can select "Run -> Run file as script" (or Ctrl+Shift+E), but this will also restart the shell.
This way it works as expected, although there is actually one inconvenience here if your macro starts with: sel=Gui.Selection.getSelection(). It's a shame that "Run file as script" needs restarting the shell.
I will use the alternative method with inspect.currentframe() as per your instructions if needed. I should make a note of this somewhere so I don't forget.

By the way, I've been taking a look at the Pyzo GitHub, and I can see that you've made a lot of improvements regarding QT, among other things, for the latest release (congratulations on such great work!). I wanted to ask you, based on what I can see, do we still need the line QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts, True) at the startUp script of the shell to avoid some annoying messages when starting the FreeCAD instance, or am I missing something and it's no longer necessary?
Post Reply