Python Debugger

25 views
Skip to first unread message

Jay Fletcher

unread,
Nov 19, 2024, 10:46:34 AM11/19/24
to GEDitCOM II Discussions
I would like to use a debugger while I am modifying the "Find and Merge Records" python script. What would be the best way to do this? I have installed the latest version of Python (3.13) on my Mac, including the IDLE IDE.

I can't find "Find and Merge Records.py". It seems to be imbedded in "MergeRecords.gplug".

If I copy the "Find and Merge Records"  code from the GEDitCOM II Editor into IDLE I get errors in the IDLE shell when I try to run it from there as follows:

Python 3.13.0 (v3.13.0:60403a5409f, Oct  7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.

========== RESTART: /Users/jay/Documents/Find and Merge Duplicates.py ==========
Traceback (most recent call last):
  File "/Users/jay/Documents/Find and Merge Duplicates.py", line 10, in <module>
    from Foundation import *
ModuleNotFoundError: No module named 'Foundation'

I am new to using Python, so perhaps I just need to set some paths to the required modules?

Any help is appreciated. Thank you,
Jay

Jay Fletcher

unread,
Nov 20, 2024, 12:16:45 PM11/20/24
to GEDitCOM II Discussions
I determined that I needed to install the scripting bridge for Python for the new version of Python which allowed me to get past the error above. I then tried to run "Find and Merge Records.py" again from within IDLE, and I got the following error:

Traceback (most recent call last):
  File "/Users/jay/Documents/Find and Merge Duplicates.py", line 13, in <module>
    from GEDitCOMII import *
ModuleNotFoundError: No module named 'GEDitCOMII'

So, I moved a copy of 'GEDitCOMII.py' to my working directory (Documents) and tried again. This resulted in the following:

The script 'Find and Merge Duplicates' requires GEDitCOM II, Version 1.6, build 2 or newer.
Please upgrade and try again.

So there appear to be other dependencies to run a Python script from within GEDitCOM that I cannot satisfy with the approach I am taking. It appears that a more efficient approach to use the debugger would be to launch IDLE from within GEDitCOM II. Is this possible?

Thank you,
Jay

Richard Blake

unread,
Nov 20, 2024, 12:54:25 PM11/20/24
to geditcom-ii...@googlegroups.com
Hi Jay

I am doing something very similar to you to run/debug Python scripts from within an IDE. Although I use PyCharm. Having been through a similar set of issues to you, I am now working OK.

The last message you mentioned is from the 'GEDitCOMII' script within the CheckVersionAndDocument function. 

I presume that GEDitCOMII is open when you run the script. I can't say for certain what the issue is but it sounds like this bit isn't returning the correct version number (vnum)

_gedit = SBApplication.applicationWithBundleIdentifier_(appID)
_scriptName = sName

vnum = _gedit.versionNumber()
Can you debug that and see what vnum is set to?

Interested to hear how you get on.

Richard

--
You received this message because you are subscribed to the Google Groups "GEDitCOM II Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geditcom-ii-discu...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/geditcom-ii-discussions/37cde6e7-1569-48db-8f1d-82761d127666n%40googlegroups.com.

John Nairn

unread,
Nov 20, 2024, 2:36:59 PM11/20/24
to geditcom-ii...@googlegroups.com
Hi Richard,

I wrote this simple script

# Prepare to use Apple's Scripting Bridge for Python
from Foundation import *
from ScriptingBridge import *

# fetch application object
appID = "com.geditcom.GEDitCOMII"
gedit = SBApplication.applicationWithBundleIdentifier_(appID)
vnum = gedit.versionNumber()
bnum = gedit.buildNumber()
print("version "+str(vnum)+" build "+str(bnum))
quit()

and it prints this:

     version 3.200000047683716 build 6

Is that what you meant by incorrect number? My scripting methods defines version number as a float. It is possible python decide to make it a double which causes digits after 7 significant digits to be random numbers.

The CheckVersionAndDocment() method in the Python module for GEDitCOM II has 3 purposes:

1. Check current version is new enough for your script. This check is not needed when you are writing your own scripts. It is only needed if you give scripts to others and need to verify they have correct version of GEDitCOM II. You need a version that supports any GEDitCOM II scripting feature needed in your script.

2. Check at least one doument is open. Most scripts need a document, although it is easy to check yourself.

3. Grab application object save it in a global variable for the  GEDitCOMII.py module. The method also returns this object.

The last one is actually the most import and why all scripts that use the GEDItCOMII python module should start with that method. Some other methods in the GEDItCOMII.py module use that global.

I usually write internal scripts (although it lacks advanced programming methods available in python). When it is enough, it is much faster. I also worry Apple will drop python support. I recently found a problem in python caused by the latest MacOS (Sequioia). I am working on a fix. The above script as an internal script is

write "version "&@gcapp.versionNumber&" build "&@gcapp.buildNumber&return
exit

The application object is created for you. This prints:

     version 3.2 build 6 

John

Richard Blake

unread,
Nov 20, 2024, 2:58:13 PM11/20/24
to geditcom-ii...@googlegroups.com
Hi John

When I run scripts within PyCharm and check the vnum variable I get the same as you. What I was trying to do was to help Jay to figure out why things are not working in his environment. It looks like he is not getting the same value returned by 
gedit = SBApplication.applicationWithBundleIdentifier_(appID)
vnum = gedit.versionNumber()
which explains the message being reported. As I've never used IDLE I can't say what the issue is but just pointing out things to check which may help to narrow down the problem.

Regards

Richard

Jay Fletcher

unread,
Nov 20, 2024, 10:46:44 PM11/20/24
to GEDitCOM II Discussions
Hi John and Richard,

Thank you for your responses to my questions.

I am indeed trying to edit, debug, and run a python script to manipulate data in GEDitCOM II using the IDLE integrated development environment provided with the Python distribution for Mac. I’m doing this so that I can use a debugger.

I copied the script provided by John into the GEDitCOM II Editor and ran it with the following results:

version 3.200000047683716 build 5


When I copy it into the IDLE shell and run it I get the following:


============================== RESTART: /Users/jay/Documents/Get GEDitCOM II version.py =============================

version 0.0 build 0


I get this result whether or not the GEDitCOM II application is open.


Do you have any idea why I get a different result?


I have installed the scripting bridge for Python to work with Python version 3.13 and I placed the GEDitCOM II.py module in my working directory and they both appear to be loading correctly.


The reason I am using IDLE instead of the GEDitCOM II Editor is so that I can run the script in a debugging mode to trace execution and examine variable values. If there is a way to invoke a debugger from within the GEDitCOM II Editor, I would be open to trying that as well.


Thank you,

Jay

Richard Blake

unread,
Nov 21, 2024, 3:31:22 PM11/21/24
to geditcom-ii...@googlegroups.com
Hi Jay

I've taken the script that John supplied and have run it both in IDLE and PyCharm. In PyCharm I get the expected result (version 3.200000047683716 build 5) but in IDLE I get 'version 0.0 build 0' the same as you. I have made sure that both are using the same Python installation and therefore are using the same installed modules. 

I really can't explain why this is not working in IDLE and as it is not an IDE that I have used before I really don't know what to check next.

Sorry I can't be of more help.

Richard


Jay Fletcher

unread,
Nov 21, 2024, 4:04:01 PM11/21/24
to GEDitCOM II Discussions
Hi Richard,

Thanks for your reply.

I had several python installations on my Mac, which I think was causing me some confusion. I removed all of them except what I think is the version that comes with MacOS. This is Python 3.9.6. When I type "python3 --version" in terminal, the result is "Python 3.9.6". The path to this version is /usr/bin/python3.

I have verified that I can still run John's python script and I still get the same result when I run if from the GEDitCOM II editor, "version 3.200000047683716 build 5". I think this means that the Scripting Bridge is correctly installed with this version of python. I have also verified that the version of Python that executes when I run this script from the GEDitCOM II Editor is version 3.9.6.

I installed PyCharm so that I could compare results with you directly. I started a new project in PyCharm called "GEDitCOMII_version". The python version /usr/bin/python3 was automatically selected. I dragged and dropped John's python script file, which I named, "GEDitCOMII_version.py", onto the PyCharm IDE. I then tried to run the script, but I get the following error in the console window:

/Users/jay/PycharmProjects/GEDitCOMII_version/.venv/bin/python /Users/jay/GEDitCOMII_version.py
Traceback (most recent call last):
  File "/Users/jay/GEDitCOMII_version.py", line 10, in <module>

    from Foundation import *
ModuleNotFoundError: No module named 'Foundation'
Screenshot 2024-11-21 at 12.57.29 PM.png
This doesn't make sense to me because I know that the Scripting Bridge has been installed to work with Python 3.9.6 because it works when I run the script from the GEDitCOM II Editor.

Did you have this problem initially using PyCharm? If so, do you know how to resolve it? Do you have any other suggestions on how to get python working with PyCharm?

Thank you,
Jay

Richard Blake

unread,
Nov 21, 2024, 4:20:07 PM11/21/24
to geditcom-ii...@googlegroups.com
Hi Jay

It's all about how you set up your project in PyCharm. If you look at the console (your second screenshot), you will see that a virtual environment has been set up for your project. This is in the .venv hidden directory where your source code is. You will need to ensure that the foundation and scripting bridge modules are installed in this environment.

Check the installed packages here:

PastedGraphic-1.png

If pyobjc is not listed as installed then search for it using the search box and install it from PyPI. It should then all work.

Regards

Richard


On 21 Nov 2024, at 21:04, Jay Fletcher <jaywfl...@gmail.com> wrote:

Hi Richard,

Thanks for your reply.

I had several python installations on my Mac, which I think was causing me some confusion. I removed all of them except what I think is the version that comes with MacOS. This is Python 3.9.6. When I type "python3 --version" in terminal, the result is "Python 3.9.6". The path to this version is /usr/bin/python3.

I have verified that I can still run John's python script and I still get the same result when I run if from the GEDitCOM II editor, "version 3.200000047683716 build 5". I think this means that the Scripting Bridge is correctly installed with this version of python. I have also verified that the version of Python that executes when I run this script from the GEDitCOM II Editor is version 3.9.6.

I installed PyCharm so that I could compare results with you directly. I started a new project in PyCharm called "GEDitCOMII_version". The python version /usr/bin/python3 was automatically selected. I dragged and dropped John's python script file, which I named, "GEDitCOMII_version.py", onto the PyCharm IDE. I then tried to run the script, but I get the following error in the console window:

/Users/jay/PycharmProjects/GEDitCOMII_version/.venv/bin/python /Users/jay/GEDitCOMII_version.py
Traceback (most recent call last):
  File "/Users/jay/GEDitCOMII_version.py", line 10, in <module>
    from Foundation import *
ModuleNotFoundError: No module named 'Foundation'

Jay Fletcher

unread,
Nov 21, 2024, 4:40:31 PM11/21/24
to GEDitCOM II Discussions
Success!

/Users/jay/PycharmProjects/GEDitCOMII_version/.venv/bin/python /Users/jay/GEDitCOMII_version.py
version 3.200000047683716 build 5

Process finished with exit code 0

Thank you very much, Richard!

Jay

Reply all
Reply to author
Forward
0 new messages