Import python files from other directories in google colaboratory

So I have searched this, and there were similar questions, but their solutions don't work for me. I have cloned a code from github on google colaboratory, the import section works fine on my laptop, which imports .py files from other directories (you have to go two folders back and then enter another folder to import the py file). but on colab, it gives me the error of "no module named ex1" when importing the python file. I tried adding

sys.path.insert(0,'path to directory')

to the directory that the file is being imported from but its not working.

the main python code is at: ex1/ex2/ex3/

which imports: from ex1.ex4 import foo

so I add the following code to the main file:

sys.path.insert(0,'path to ex1')

What am I doing wrong here?

Read more here:

Content Attribution

This content was originally published by sandra at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: