FreeCAD 0.18 Python3 bindings

Need help, or want to share a macro? Post here!
donjan
Posts: 25
Joined: Wed Jun 21, 2017 11:40 am

FreeCAD 0.18 Python3 bindings

Postby donjan » Wed Jan 23, 2019 6:38 pm

Testing 0.18 from the daily repo on Ubuntu:

Code: Select all

$ export PYTHONPATH=/usr/lib/freecad-daily/lib && python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import FreeCAD
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_FreeCAD)
It works as usual when invoking python2 instead.
The console mode also goes for Python2:

Code: Select all

$ /usr/lib/freecad-daily/bin/FreeCADCmd
[SNIP]
>>> import sys; print(sys.version)
2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0]
Other libs I'd like to import in the same scripts are dropping Python2 support soon (https://python3statement.org/), or already have done so (numpy and FEniCS in my case).
Can the daily be built with Python3 bindings (pretty please)?
Or is there another method to monkey-patch this short of building FreeCAD myself?
User avatar
kkremitzki
Posts: 1709
Joined: Thu Mar 03, 2016 9:52 pm
Location: Texas

Re: FreeCAD 0.18 Python3 bindings

Postby kkremitzki » Thu Jan 24, 2019 12:11 am

It's coming soon. I have an experimental dual-build Python 2 & 3 FreeCAD 0.18 available in my personal PPA at https://launchpad.net/~kkremitzki/+archive/ubuntu/ppa but you may run into issues.
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.
donjan
Posts: 25
Joined: Wed Jun 21, 2017 11:40 am

Re: FreeCAD 0.18 Python3 bindings

Postby donjan » Thu Jan 24, 2019 4:23 pm

After purging the main repo package and installing from your PPA, I get:

Code: Select all

$ export PYTHONPATH=/usr/lib/freecad/lib && python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import FreeCAD
FreeCAD 0.18, Libs: 0.18R
No modules found in /usr/lib/freecad-python3/Mod
During initialization the error No module named 'freecad' occurred
Now despite the error I can load the .so modules and do some work:

Code: Select all

>>> doc = FreeCAD.newDocument('testDoc')
>>> doc.load('/home/donjan/Desktop/test.fcstd')
<unknown exception traceback><class 'ModuleNotFoundError'>: No module named 'BOPTools'
<unknown exception traceback><class 'ModuleNotFoundError'>: No module named 'CompoundTools'
Importing project files......
>>> print(doc.Objects[0].Label)
Box
But the Python modules have issues:

Code: Select all

>>> import Draft
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Draft'
The import Draft error can be fixed with:

Code: Select all

sudo ln -s /usr/lib/freecad/Mod /usr/lib/freecad-python3/Mod
The "During initialization the error No module named 'freecad' occurred" error can be disappeared with:

Code: Select all

sudo ln -s /usr/lib/freecad/Ext /usr/lib/freecad-python3/Ext
It works for my purposes right now, but no idea how shaky all of this is :)
Thanks!
And soon == ?
jvs
Posts: 1
Joined: Sat Sep 28, 2019 9:19 pm

Re: FreeCAD 0.18 Python3 bindings

Postby jvs » Sat Sep 28, 2019 9:23 pm

I had the same problem. The 2 symlinks fixed it. Thanks!