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.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
fosselius
Posts: 381
Joined: Sat Apr 23, 2016 10:03 am
Contact:

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

Post by fosselius »

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: 436
Joined: Wed Sep 15, 2010 9:38 am

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

Post by galou_breizh »

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
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

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

Post by triplus »

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.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

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

Post by looo »

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.
Post Reply