Help Requested regarding python configuration

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
falviani
Posts: 253
Joined: Tue May 07, 2019 8:49 pm

Help Requested regarding python configuration

Post by falviani »

Hi All,

This is a duplicate post from the developer section of this forum, as I got no replies after several days, and I'm hoping this is a better place to ask this question.

Extended info regarding my setup configuration, including python:
Weekly FC version:
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.25645 (Git)
Build type: Release
Branch: master
Hash: 37d9757399b4c2ec30318eb88d7cd7c508246345
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United States (en_US)

There are 2 versions of python installed in the Appdata/Local/Programs folder, as shown in the user-specific environment variables:
C:\Users\frank\AppData\Local\Programs\Python\Python38\Scripts\
C:\Users\frank\AppData\Local\Programs\Python\Python38\
C:\Users\frank\AppData\Local\Programs\Python\Python37-32\Scripts\
C:\Users\frank\AppData\Local\Programs\Python\Python37-32\

There is an old version of Python (Python 3.5) installed in it's own folder.

I installed debug.py while CDed into the appropriate FreeCad bin folder; it was installed in the python38 site-files folder.

In the workbench init.py folder I have the following suggested code:

Code: Select all

import debugpy
debugpy.configure(python="python")
debugpy.listen(5678)
debugpy.trace_this_thread(True)
debugpy.debug_this_thread()
In a previous question, it was suggested that I use the following configuration in the launch.json file:

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
          }
    ]
}
If I use this as is, I just get a 'connection refused error

In a previous version of the launch file, I would get the following error when I tried to debug the running app:

Code: Select all

"configurations": [     
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "E:\\FreeCAD\\FreeCAD_0.20.25645_Win-LPv12.5.4_vc17.x-x86-64\\FreeCAD_0.20.25645_Win-LPv12.5.4_vc17.x-x86-64\\bin\\FreeCAD.exe",
            "console": "integratedTerminal"
        }
    ]
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

My question is: Is there an issue with my python configuration? If so, how should I correct it so I can debug the workbench I am attempting to create?

I am a retired programmer with a variety of experience, but my professional experience was in C# for the last 13 years of my career. I did some Python on the side, but not a huge amount, and I am refreshing my knowledge of the language. The available MS documentation on debugging Python in VS Code doesn't match what I see in my version of VSC at all, and isn't useful. I did read the wiki chapter on creating workbenches, and I based my code on the starter kit by Joel Graff. I have spent several days trying to track down the source of the error, and a fix, with no success. I must be doing something quite wrong, as all suggestions I have received haven't worked for me. I plan to study the links given in the intro to this section, but I'd like to at least get this issue resolved.

All suggestions will be greatly appreciated.
-Frank Alviani
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Help Requested regarding python configuration

Post by TheMarkster »

I think with VS Code you must add some code to your script. I use Visual Studio 2019 for debugging python. When installing I added everything python related that was offered. I get FreeCAD running first, and then in the Debug menu I choose attach to process, attach to python code, select FreeCAD. I have some trouble with 0.20 detaching itself and crashing, but 0.19 works very well with it. In fact, you might say it's pretty awesome.
Sometimes you might need to remove and reset a breakpoint after editing the code or else it breaks on the wrong line.
falviani
Posts: 253
Joined: Tue May 07, 2019 8:49 pm

Re: Help Requested regarding python configuration

Post by falviani »

Thank you SO much for your suggestion! I had to straighten out my directory structure, but am now able to place breakpoints (although the debugger seems to want a 'frozen zipfile' for some reason when it hits a breakpoint. I can now make some progress!
Post Reply