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

**URL:** <https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071>\
**Category:** Developers\
**Tags:** python\
**Created:** [September 11, 2020, 7:43pm UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071 "2020-09-11T19:43:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![infeeeee](https://avatars.discourse-cdn.com/v4/letter/i/e480ec/32.png) [@infeeeee](https://forum.dynamobim.com/u/infeeeee)\
**Post date:** [September 11, 2020, 7:43pm UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071/1 "2020-09-11T19:43:03Z")

</div>

I’m working on wrapping the FreeCAD python API in custom nodes: [GitHub - infeeeee/DynFreeCAD: Dynamo nodes for FreeCAD](https://github.com/infeeeee/DynFreeCAD)

> FreeCAD can be imported as a [Python](https://wiki.freecadweb.org/Python) module in other programs or in a standalone Python console, together with all its modules and components.

[https://wiki.freecadweb.org/Embedding\_FreeCAD](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:  
 ![01 - ModuleNotFound](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/d/ed3413fc4e16f6dc3459c0f096fb0db14728220b.png)  
This is sys path, as you can see all FreeCAD folders added successfully:  
 ![kvdpAcAsXt](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/a/8adf5494d99a3c16f5abeea5e4957bfebada5da9.png)
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:  
 ![02 - Restart dynamo, import in first node](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/c/4cb6880f4811cf8a8774071c56016b9959fc0b8b.png)
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!  
 ![03 - reopen script, import works in second node](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/7/17e8c7162fafb553c2d225b1413ae71cfcf610fa.png)

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](https://forum.dynamobim.com/t/cpython3-warning-cannot-convert-object-to-target-type/54369)

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.

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [September 12, 2020, 12:25am UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071/2 "2020-09-12T00:25:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michael\_Kirschner2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/michael_kirschner2/32/75576_2.png) [@Michael\_Kirschner2](https://forum.dynamobim.com/u/Michael_Kirschner2)\
**Post date:** [September 12, 2020, 1:33am UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071/3 "2020-09-12T01:33:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![infeeeee](https://avatars.discourse-cdn.com/v4/letter/i/e480ec/32.png) [@infeeeee](https://forum.dynamobim.com/u/infeeeee)\
**Post date:** [September 14, 2020, 8:13am UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071/4 "2020-09-14T08:13:26Z")

</div>

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.

 ![kép](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/0/c06112f6dcc519793941459edecc8eb0f032f76d.png)

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.

---

<div class="post-metadata">

**Author:** ![infeeeee](https://avatars.discourse-cdn.com/v4/letter/i/e480ec/32.png) [@infeeeee](https://forum.dynamobim.com/u/infeeeee)\
**Post date:** [September 14, 2020, 8:30am UTC](https://forum.dynamobim.com/t/how-to-use-import-from-custom-path-in-multiple-cpython-blocks/55071/5 "2020-09-14T08:30:41Z")

</div>

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](https://wiki.qt.io/Qt_for_Python) 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.

> **[Embedding FreeCAD - FreeCAD Documentation](https://wiki.freecadweb.org/Embedding_FreeCAD#Using_FreeCAD_with_GUI)**
