# How to import an external python file

**URL:** <https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895>\
**Category:** Developers\
**Created:** [March 11, 2018, 7:48pm UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895 "2018-03-11T19:48:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedrorocha](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pedrorocha](https://forum.dynamobim.com/u/pedrorocha)\
**Post date:** [March 11, 2018, 7:48pm UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/1 "2018-03-11T19:48:18Z")

</div>

Hello guys!

I am writing a code in a python script node and I have to import a .py file from a directory on my computer in order to use the class functions which are in this external .py file.  
Can anyone help me on how to do it?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [March 12, 2018, 2:03am UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/2 "2018-03-12T02:03:50Z")

</div>

I am sure there is a way to do this in python but a node way might be to import it into a code block as text and then use python script from string node. Not 100% sure it can work but might be worth a try.

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [March 12, 2018, 2:11am UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/3 "2018-03-12T02:11:31Z")

</div>

See the dynamo dictionary here: [http://dictionary.dynamobim.com/#/Core/Scripting/Action/Python](http://dictionary.dynamobim.com/#/Core/Scripting/Action/Python) Script From String

The web page has spaces in the url and I can’t copy and paste the right URL, so look for the Python Script From String node and read the example, it does what you are asking for.

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [March 12, 2018, 9:16am UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/4 "2018-03-12T09:16:49Z")

</div>

Hi Pedro  
See below:

 ![12-03-2018%2010-16-12](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/0/30452fda605cb70e1a9697428678ffbaab88b077.png)

---

<div class="post-metadata">

**Author:** ![pedrorocha](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pedrorocha](https://forum.dynamobim.com/u/pedrorocha)\
**Post date:** [March 12, 2018, 3:27pm UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/6 "2018-03-12T15:27:53Z")

</div>

@kennyb6 and @Jonathan.Olesen

Thank you for answering. I got inspiration from your tips and solved my problem as shown below:

 ![importsol](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/3/93dee093f4b213e913149de50f9f8223eccd870e.png)

By this way I could use pre-defined functions from an external python file in a new one that i’m currently working on.

Thanks!

Pedro

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [March 13, 2018, 1:45am UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/8 "2018-03-13T01:45:36Z")

</div>

That’s an interesting solution 😃

There’s generally two ways to load external python files that I know of. The preferred way is to load it as a module, by adding its path to the lookup paths of the sys module:

 ![notepad%20%20_2018-03-13_09-42-09](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/8/d84c4fd0e9c05673588cac1faa276ce535912260.png)

The alternative is to directly execute all of the file in the current scope, which while simpler, can be a lot harder to maintain:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/7/375596e7daf850f9836ae38525316d33642824c9.png)

---

<div class="post-metadata">

**Author:** ![jw.vanasselt](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jw.vanasselt/32/133533_2.png) [@jw.vanasselt](https://forum.dynamobim.com/u/jw.vanasselt)\
**Post date:** [November 14, 2022, 11:30am UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/9 "2022-11-14T11:30:25Z")

</div>

@Dimitar_Venkov ,

Old topic but I try to use a pyfile for my functions.  
It doesn’t work, how can I use “defs” from an other file?  
I want to append it inside my script.

KG,

JW

---

<div class="post-metadata">

**Author:** ![jw.vanasselt](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jw.vanasselt/32/133533_2.png) [@jw.vanasselt](https://forum.dynamobim.com/u/jw.vanasselt)\
**Post date:** [November 14, 2022, 12:40pm UTC](https://forum.dynamobim.com/t/how-to-import-an-external-python-file/19895/10 "2022-11-14T12:40:25Z")

</div>

Got it! 🙂

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/8/88aa8bc978dcc0610677c2a1b008b2881eff0a7b.png)
