How to use import from custom path in multiple cpython blocks?

I’m working on wrapping the FreeCAD python API in custom nodes: GitHub - infeeeee/DynFreeCAD: Dynamo nodes for FreeCAD

FreeCAD can be imported as a Python module in other programs or in a standalone Python console, together with all its modules and components.

https://wiki.freecadweb.org/Embedding_FreeCAD

I append the FreeCAD bin folder to sys.path, and everything is working if I call other submodules inside the same python node. But if I want to import another submodule in another python node, I get ModuleNotFoundErrors.

The strange thing is, if I import the submodule in a first node, then close the graph, and in another graph I import the same submodule later, it will find it.

I don’t know if it’s understandable what is my problem, so here how I can reproduce this bug:

  1. First I open this simple graph. I get ModuleNotFoundError:

    This is sys path, as you can see all FreeCAD folders added successfully:
  2. After this I close Dynamo, and open it again, but this time I modify the graph to import in the first python node. Now it finds the Mesh submodule:
  3. After this I close the graph, (not dynamo just the graph!) and if I load the original script, where I only load the submodule in the second node, it works!

As I want to create separate nodes for separate FreeCAD commands, I want to import only on the node, where I need that module. Am I doing something wrong, or I found some bug in Dynamo? How I should import modules from custom path?

I can reproduce this bug in Dynamo 2.8 and 2.9. I can’t use 2.7 because of this bug: CPython3: Warning: cannot convert object to target type

As a workaround I was thinking that I could import everything in the first node, but FreeCAD has around 50 submodules, and even more mods, and it won’t work if someone want to use a non-default mod.

2 Likes

I don’t know how to solve this offhand, but I love the idea.

Have you considered doing this via a Zero Touch method or even an integration of Dynamo into freecad?

try modifying sys path to include the free cad modules in every python node you use free cad modules in.

1 Like

Didn’t help, same ModuleNotFoundError. For this screenshot: modified the file, restarted dynamo, reopened graph, clicked run, so I didn’t just appended the path and clicked run again.

Or do I misunderstood something? Can I check in some logs what’s going on? I didn’t find any usable info in the logs in %appdata%., Can I setup higher log levels somewhere?

Edit: I wanted to reply this to @Michael_Kirschner2 but I clicked the wrong reply button.

I’m not familiar in C# development, I’m still learning python, one new language is enough for me for now.

FreeCAD’s gui is written in Qt, and can be embedded into other applications, so I’m planning to integrate it the otherways: to start FreeCAD from dynamo, similarly to how DynaWorks works. I already started working this, but I think the pyside2 version for dynamo’s python version and for FreeCAD’s python version is not compatible to each other or something like that. So I would like to solve other issues first.

1 Like