Subfolder for Python modules

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
kkremitzki
Veteran
Posts: 2511
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Subfolder for Python modules

Post by kkremitzki »

looo wrote:One thing that came into my mind:

Code: Select all

import Part
will become

Code: Select all

from freecad.modules import Part
and if someone uses the console quite often this is maybe a bit annoying...
On my system I use a .pythonrc file to define quick setup functions I can run any time I launch a Python interpreter.. If we look for a PYTHONSTARTUP environment variable people could put that sort of thing in there.

It's also a great place for things like from __future__ import division which you might want to run every time to "augment" your console.
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.
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: Subfolder for Python modules

Post by ian.rees »

looo wrote:One thing that came into my mind:

Code: Select all

import Part
will become

Code: Select all

from freecad.modules import Part
and if someone uses the console quite often this is maybe a bit annoying...
We could add a bit of code that gets run when a workbench is activated, that does this automatically. For example, maybe when you switch to Part in the GUI it automatically runs

Code: Select all

from freecad.modules import Part
-Ian-
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Subfolder for Python modules

Post by looo »

Good points.
I like the idea of calling a file with some user defined imports more. If someone writes a macro he should use the full imports (from freecad.modules). At least if this macro should work on other systems too. So if someone wants to use special syntax (import Part) he/she has to be aware of the fact that this syntax is not supported for other users. So a user-specific startup-file is the better option.
Last edited by looo on Thu Mar 23, 2017 9:38 am, edited 2 times in total.
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

looo wrote:first try fails (python3):

Code: Select all

Error in FreeCADGuiInit.py: Parent module 'freecad.modules.Arch' not loaded, cannot perform relative import
Traceback (most recent call last):
  File "<string>", line 158, in <module>
  File "<string>", line 142, in InitApplications
  File "/home/lo/anaconda/envs/freecad/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 981, in _gcd_import
  File "<frozen importlib._bootstrap>", line 931, in _sanity_check
<class 'SystemError'>: Parent module 'freecad.modules.Arch' not loaded, cannot perform relative import
Thanks for the pointer, I haven't tried this branch or FreeCAD in general with Python 3 yet, I thought it didn't work with Python3 yet.
All the testing I've done has been with Python 2.
I'll fix this one though, makes obviously more sense to make sure that new code is Python 3 compatible.

[edit] Should be fixed now :)

Also, regarding the imports, yes there will be more letters/words because you lose some magic, but as suggested if you really want to have simplified aliases available you can still add that in multiple ways. Both the automatic import on workbench activation as well as a user configurable option using PYTHONSTARTUP sound like good suggestions.

Also the REPL in FreeCAD GUI already support tab completion [edit] Just noticed this doesn't work for imports for some reason :([/edit]
The command line one doesn't, but integrating something like ptpython should be pretty straightforward.
looo wrote:thanks, currently building.
simonvanderveldt wrote:One thing I did run into was this error when starting the Arch workbench
This isn't a big problem. Removing the magic parts from FreeCAD will really help people understand the code. And function which work without import are quite magic ;)
This could become annoying though if there a modules that use this extensively. Ideally the logging shouldn't work with aliased/global values like this, but just use Python's native logging API, but that's something for another time ;)
I think such things can only occur in Init.py and InitGui.py.
You're right, I only found this in Arch's GUI init, fixed it by making the imports explicit
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

It seems like FreeCAD modules aren't required to have both an init and a GUI init module. I'm not sure if this is by design or not, but for now I've updated the code so it first checks if the relevant init module exists. If it does it'll be initialized, if it doesn't a message will be logged that no init module exists for that FreeCAD module.
This way it's also possible to distinguish between proper ImportErrors and a non existing init modules :)
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Subfolder for Python modules

Post by looo »

My understanding is that the Init.py is for initialization without gui. This gives the possibility to send information to FreeCAD when FreeCAD is used as a non-gui library. (eg.: information about supported file formats, but I don't know exactly)

If this is true, it should be called when FreeCAD is imported. But I don't think this is done with the current system. Maybe we should think about this too. Maybe someone with more experience can explain why the Init.py is needed.
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

looo wrote:My understanding is that the Init.py is for initialization without gui. This gives the possibility to send information to FreeCAD when FreeCAD is used as a non-gui library. (eg.: information about supported file formats, but I don't know exactly)
Yeah, this is also what I gathered. Init.py seems to be used to register filetypes to open/write to for example.
looo wrote:If this is true, it should be called when FreeCAD is imported. But I don't think this is done with the current system. Maybe we should think about this too. Maybe someone with more experience can explain why the Init.py is needed.
Agreed, no need to have a separate init when it can just be handled by __init__.py.
This does go into the somewhat larger question of how to create a decent plugin/extension architecture. Right now it' pretty crude, but simple, if an Init.py or InitGui.py exists in a modules then they are being executed by the relevant (non-)GUI FreeCAD init script.
I think that even though changing this would be nice it'd propose to do so at a later point and keep these initial changes small. I'd expect it to be a lot more work to change the initialisation, mainly because there are currently 2 separate init steps (non-GUI and GUI) and ideally you'd just want to import the module, let the module check during import if it's being imported in a GUI session and execute the relevant code.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Subfolder for Python modules

Post by DeepSOIC »

Wait a second... many add-on workbenches have a dash in their name. Like "Part-o-magic", "dxf-library", "freecad-nurbs"... Does it mean they have to be renamed?
simonvanderveldt
Posts: 62
Joined: Tue Mar 14, 2017 2:11 pm

Re: Subfolder for Python modules

Post by simonvanderveldt »

DeepSOIC wrote:Wait a second... many add-on workbenches have a dash in their name. Like "Part-o-magic", "dxf-library", "freecad-nurbs"... Does it mean they have to be renamed?
If they are to be normal Python models then there shouldn't be dashes/hyphens in their name, see https://stackoverflow.com/questions/761 ... mport-them

As can be seen in the answers/comments there are workarounds to make it work, but that you can do so doesn't mean you should :)


P.S. The current code I've written should handle modules with hyphens just fine (haven't tested it though), but all consumers of these modules be it the module's own code or something else would need to use one of the workarounds to make it work.
Last edited by simonvanderveldt on Thu Mar 23, 2017 10:43 pm, edited 1 time in total.
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Subfolder for Python modules

Post by sgrogan »

simonvanderveldt wrote:If they are to be normal Python models then there shouldn't be dashes/hyphens in their name, see https://stackoverflow.com/questions/761 ... mport-them
First I think making FreeCAD more Pythonic is a very FreeCADic.
@simonvanderveldt, can you post an outline of what the transition would look like at the moment, similar to what looo did previously? Including add-on WB/Modules and macros.
"fight the good fight"
Post Reply