[SOLVED] ModuleNotFoundError / ImportError - FC 0.20 Macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
bensenior
Posts: 29
Joined: Mon Dec 07, 2020 7:55 pm

[SOLVED] ModuleNotFoundError / ImportError - FC 0.20 Macro

Post by bensenior »

Hello,
This is a question about trying to use the Legify Macro from vecronic (https://github.com/vectronic/freecad-legify-macros).

I'm using the weekly dev build AppImage on Ubuntu 21.10, placing the legify-brick.FCMacro in ~/.local/share/FreeCAD/Macro/ and the accompanying Legify package in the same location (i.e. ...Macro/Legify/[stuff]).

When I try to execute the macro in FC, I get the error :

Code: Select all

<class 'ModuleNotFoundError'>: No module named 'Legify'
Traceback (most recent call last):
  File "/home/ben/.local/share/FreeCAD/Macro/legify-brick.FCMacro", line 6, in <module>
    from Legify.Dialog import *
<class 'ModuleNotFoundError'>: No module named 'Legify'
The import statements in legify-brick.FCMacro are :

Code: Select all

# coding: UTF-8

from FreeCAD import Console, Gui
from PySide2.QtWidgets import QMessageBox
from PySide2.QtCore import Qt
from Legify.Dialog import *
If I try to modify the import statement (I really know *nothing* about Python besides the last two hour's digging around trying to figure out what's broken) for Legify to something like

Code: Select all

from .Legify.Dialog import *
to make the import relative and explicit (?!?) I then get the error :

Code: Select all

<class 'ImportError'>: attempted relative import with no known parent package
Traceback (most recent call last):
  File "/home/ben/.local/share/FreeCAD/Macro/legify-brick.FCMacro", line 6, in <module>
    from . Legify.Dialog import *
Can somebody please help point me in the right direction? It would be so great to have some Lego Macros around for rapid prototyping :-)

Ben
Last edited by bensenior on Sun Jan 23, 2022 5:45 pm, edited 2 times in total.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Post by onekk »

usually you have to add the path to the library to FreeCAD.

some code that could help is:

Code: Select all

MODULE_PATH = u"/your/module/position/"

if not MODULE_PATH in sys.path:
    print("module path is not present, loading....")	
    sys.path.insert(-1,MODULE_PATH)
else:
    print("module path is already present")
if you put this at top of your code it will ad the module path to sys.path and FreeCAD will find it when searching.

Hope it helps

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
bensenior
Posts: 29
Joined: Mon Dec 07, 2020 7:55 pm

Re: [SOLVED - but not understood] ModuleNotFoundError / ImportError - FC 0.20 Macro

Post by bensenior »

Yes, thank you very much :) !

I'm not sure if it's something that's perhaps wonky in the weekly build, but I actually needed to set the module path to

Code: Select all

MODULE_PATH = "/home/ben/.local/share/FreeCAD/Macro/"
Given that that's supposed to be the default macro path, could it be that the default sys.path for FreeCAD isn't quite setup correctly?
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [SOLVED] ModuleNotFoundError / ImportError - FC 0.20 Macro

Post by onekk »

Not exactly.

This is the default path that FreeCAD will automatically use for Macro.

It is different from the path used by WB that is in the a subdir of the "/Mod/" user directory and usually is in "sys.path"

Concepts are slightly different, as Macro is punt in the Macro Folder and searched by the Macro part of FreeCAD.

When you are using scripts you are suing "directly "FreeCAD python interpreter" that has his own "search path" that is accessble with "sys.path"

try to put this code in the Python Console:

Code: Select all

import sys
print(sys.path)
import sys in console wil be not stricly necessary in a script I suppose is mandatory.


In my case I have a bunch of dirs, as running from an AppIMage, and also some "user dirs", but not "~/userdir/Macro"

Note that recently @wmayer has made some changes to the position of the stabdard user directories followint XDG standards so many of them are put in "~/.config/FreeCAD".

Mine are not as I'm using different config dirs for every version of FreeCAD I'm using, as I have 0.19.3, 0.19LS3, "0.20 latest" with Italian language and "0.20 latest" with English language, to be able to make some screenshot for my "scripting guide", I know it is a mess, but not difficult.

It suffice to set:

Code: Select all

export FREECAD_USER_HOME="~/FreeCAD/config-eng"
to set an appropriate different user directory prior to run FreeCAD binary, in my case it is in the bash script that launch AppImage.

This way you could use different languages and different version without polluting each others config dirs.

The only drawback is that you have to set Preferences, AddOns and Macro for every version, but it is not a big hassle, as in Linux symlink will help a lot to make some "common directories" at least for same version of FreeCAD, (If you mix different version it will be a big mess).

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply