Where is the .py file where FreeCAD is defined on Macos?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
nyholku
Posts: 149
Joined: Wed Dec 28, 2016 4:18 pm

Where is the .py file where FreeCAD is defined on Macos?

Post by nyholku »

Hi,

I'm writing Python scripts for FreeCAD on Macos.

My imports (like 'import FreeCAD') work when I run the code inside/under FreeCAD but in my IDE (Eclipse with PyDev) they are reported 'Unresolved'.

Naturally this is because the IDE cannot know where to look for those definitions that are embedded in the FreeCAD.app Macos bundle.

But where are they inside there so I can tell Eclipse to look for them in therre, I have not been able to find them.

I printed a complete list of Python library search paths from within FreeCAD Python console but it is bit daunting to try and error each of these possible locations so help would be appreciated:

Code: Select all

/Applications/FreeCAD.app/Contents/Mod
/Applications/FreeCAD.app/Contents/lib64
/Applications/FreeCAD.app/Contents/lib
/Applications/FreeCAD.app/Contents/Mod/Drawing
/Applications/FreeCAD.app/Contents/Mod/Inspection
/Applications/FreeCAD.app/Contents/Mod/Idf
/Applications/FreeCAD.app/Contents/Mod/Part
/Applications/FreeCAD.app/Contents/Mod/Robot
/Applications/FreeCAD.app/Contents/Mod/Arch
/Applications/FreeCAD.app/Contents/Mod/Path
/Users/nyholku/Library/Preferences/FreeCAD/Mod/.DS_Store
/Applications/FreeCAD.app/Contents/Mod/Mesh
/Applications/FreeCAD.app/Contents/Mod/Raytracing
/Applications/FreeCAD.app/Contents/Mod/Material
/Applications/FreeCAD.app/Contents/Mod/Web
/Applications/FreeCAD.app/Contents/Mod/Complete
/Applications/FreeCAD.app/Contents/Mod/Import
/Applications/FreeCAD.app/Contents/Mod/Test
/Applications/FreeCAD.app/Contents/Mod/Draft
/Applications/FreeCAD.app/Contents/Mod/Points
/Applications/FreeCAD.app/Contents/Mod/Start
/Users/nyholku/Library/Preferences/FreeCAD/Mod/cloud9
/Applications/FreeCAD.app/Contents/Mod/Spreadsheet
/Applications/FreeCAD.app/Contents/Mod/Fem
/Applications/FreeCAD.app/Contents/Mod/OpenSCAD
/Applications/FreeCAD.app/Contents/Mod/MeshPart
/Applications/FreeCAD.app/Contents/Mod/Plot
/Applications/FreeCAD.app/Contents/Mod/Ship
/Applications/FreeCAD.app/Contents/Mod/PartDesign
/Applications/FreeCAD.app/Contents/Mod/Image
/Applications/FreeCAD.app/Contents/Mod/ReverseEngineering
/Applications/FreeCAD.app/Contents/Mod/Sketcher
/Applications/FreeCAD.app/Contents/bin
/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg
/Applications/Eclipse-Neon.app/Contents/MacOS
/Applications/FreeCAD.app/Contents/lib/python27.zip
/Applications/FreeCAD.app/Contents/lib/python2.7
/Applications/FreeCAD.app/Contents/lib/python2.7/plat-darwin
/Applications/FreeCAD.app/Contents/lib/python2.7/plat-mac
/Applications/FreeCAD.app/Contents/lib/python2.7/plat-mac/lib-scriptpackages
/Applications/FreeCAD.app/Contents/lib/python2.7/lib-tk
/Applications/FreeCAD.app/Contents/lib/python2.7/lib-old
/Applications/FreeCAD.app/Contents/lib/python2.7/lib-dynload
/Applications/FreeCAD.app/Contents/lib/python2.7/site-packages
/Applications/FreeCAD.app/Contents/libexec/matplotlib/lib/python2.7/site-packages
/Applications/FreeCAD.app/Contents/libexec/numpy/nose/lib/python2.7/site-packages
/Library/Python/2.7/site-packages
/Applications/FreeCAD.app/Contents/lib/
/Users/nyholku/Library/Preferences/FreeCAD/Macro
/Applications/FreeCAD.app/Contents/Macro
/Users/nyholku/Library/Application Support/FreeCAD/Mod
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Where is the .py file where FreeCAD is defined on Macos?

Post by kkremitzki »

The problem here is that you're expecting a .py file but it's actually possible to import Python modules out of files without that extension. Try this:

Code: Select all

import sys
FCPATH = '/Applications/FreeCAD.app/Contents/lib'
sys.path.append(FCPATH)
try:
  import FreeCAD as fc
except ImportError:
  print('FreeCAD not found.')
I use Linux, so my path is different, but for me, when I do import FreeCAD, that code is coming from /usr/lib/freecad-daily/lib/FreeCAD.so. Not a .py file.
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
nyholku
Posts: 149
Joined: Wed Dec 28, 2016 4:18 pm

Re: Where is the .py file where FreeCAD is defined on Macos?

Post by nyholku »

Hi,

thanks for the reply.

I see, did not know that, though I suspected that and I have experimented with this before.

However I did have one more go with this.

So in went to 'Eclipse->Preferences->PyDev->Interpreters->Python Interpreter' and for my 'python2.7' interpreter I added under 'Library' a 'New Folder' which I pointed to '/Applications/FreeCAD.app/Contents/lib'.

I've done this before and the result is the same: I start to get pop up dialogs from the system informing that a Python interpreter has crashed. Interestingly these come from the Macos, not from Eclipse directly. Eclipse does not crash and keeps working except that the annoying system dialog is in my face. If I close Eclipse the dialog stops appearing. When I restart Eclipse the problem starts again and if I remove the '/Applications/FreeCAD.app/Contents/lib' from the Eclipse Python configuration and restart Eclipse then the problem disappears.

But now comes an interesting bit.

When I re-open the project in Eclipse miraculously the 'import FreeCAD' is now NOT flagged as unresolved and the code completion etc works. However, if I close Eclipse and restart it then I'm back in square one i.e. the import is flagged as unresolved and the code completion does not work.

So my interpretation is that probably adding '/Applications/FreeCAD.app/Contents/lib' you/we are on the right track but something in that directory makes Eclipse or Python unhappy. Could be a PyDev or Eclipse bug, maybe even a Python bug.

I guess I better head to PyDev forum next.

Thanks again.
nyholku
Posts: 149
Joined: Wed Dec 28, 2016 4:18 pm

Re: Where is the .py file where FreeCAD is defined on Macos?

Post by nyholku »

Ha!

I think I got it.

In Eclipse I need to right click on the project and select 'Properties' from the right click menu.

In the dialog that appears I need to select 'PyDev - PYTHON PATH' and in that panel under 'External Libraries' tab I need to 'Add Source Folder' and point it to '/Applications/FreeCAD.app/Contents/lib'.

And restart Eclipse.

Then the '/Applications/FreeCAD.app/Contents/lib' will appear in the 'PyDev Package Explorer' under the interpreter / 'External Libs' and the imports and code completion work correctly in the Python editors in Eclipse.

cheers Kusti
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Where is the .py file where FreeCAD is defined on Macos?

Post by kkremitzki »

Nice! It's always great to see when people come back and post what fixed their issue rather than saying "ok I fixed it" :D
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
nyholku
Posts: 149
Joined: Wed Dec 28, 2016 4:18 pm

Re: Where is the .py file where FreeCAD is defined on Macos?

Post by nyholku »

Thanks, yes I agree and that is why take a little trouble to document what worked and what not.
Post Reply