How to import an external python file

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!

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.

See the dynamo dictionary here: 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.

Hi Pedro
See below:

@kennyb6 and @Jonathan.Olesen

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

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



1 Like

try to see this…

make a real OOP environment, instead of String.Concat

That’s an interesting solution :smiley:

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:

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