Discussion: using FreeCAD or App namespaces, which is prefered

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
fosselius
Posts: 344
Joined: Sat Apr 23, 2016 10:03 am

Re: Discussion: using FreeCAD or App namespaces, which is prefered

Postby fosselius » Sun Sep 08, 2019 7:48 pm

ezzieyguywuf wrote:
Sat Sep 07, 2019 1:34 pm
is that assumption a good one? Or is this particular test poorly written, and needs to import FreeCAD itself?
No, Yes ^_^
galou_breizh
Posts: 259
Joined: Wed Sep 15, 2010 9:38 am

Re: Discussion: using FreeCAD or App namespaces, which is prefered

Postby galou_breizh » Tue Sep 10, 2019 12:29 pm

There are even another alternatives:

Code: Select all

from freecad import app
and

Code: Select all

from freecad import gui
.

These are just other places to find the modules, as a proof:

Code: Select all

>>> app is App
True
>>> gui is FreeCADGui
True
There is also

Code: Select all

import freecad
but this one is not equivalent to "FreeCAD":

Code: Select all

>>> import freecad
>>> freecad is FreeCAD
False
I wanted to used

Code: Select all

from freecad import app
in all macros of the official macro repository but some people (I think people on Windows) complained that there was no freecad module (small letters). What is currently used in the official macros is

Code: Select all

import FreeCAD as app
. Uses of "FreeCAD" and "App" are then replaced with "app". Once one of the variants settles down, this should be a matter of changing one line in each macro.

Gaël
triplus
Posts: 8676
Joined: Mon Dec 12, 2011 4:45 pm

Re: Discussion: using FreeCAD or App namespaces, which is prefered

Postby triplus » Sat Oct 05, 2019 9:17 am

In my opinion it makes sense to wait for Qt 6 and to first see what they understand under the term "introducing more Pythonic API". After i guess we can discuss if FreeCAD should have more Pythonic API, closer to Qt one, i guess Python bindings. After i guess we can discuss if new import module style needs to or should adapt more. As currently we likely wouldn't be able to decide if FreeCAD/freecad/App/app ... should be used. In addition if other libraries would do the same, for example it would not be numpy but it would be app. That likely wouldn't end up all that good. For now therefore use:

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui
That is the most consistent solution, and it aligns well with C++ counterpart. Don't assume, in your code, somebody else already did the imports.

P.S. As for the new import style modules, and the current situation, best if @looo describes what makes the most sense.
looo
Posts: 2820
Joined: Mon Nov 11, 2013 5:29 pm

Re: Discussion: using FreeCAD or App namespaces, which is prefered

Postby looo » Wed Oct 09, 2019 1:58 pm

triplus wrote:
Sat Oct 05, 2019 9:17 am
P.S. As for the new import style modules, and the current situation, best if @looo describes what makes the most sense.
I guess it's quite simple. We never fully moved towards new-style modules and we support both methods. If we would move fully towards new-style modules this will have a similar impact as the change from python2 to python3. Means that we most likely fully break backwards compatibility. Thats why I suggest to use new-style where possible and use the old way if there is no other option.

Regarding the topic, I did some experiments with "from freecad import app" and actually I don't have issues with this. But I don't see a big issue with using something like this:

Code: Select all

import FreeCAD as app
once we decide to fully switch to new-style modules (which is not guaranteed to happen) it's easy to replace the import.