Skip to content
← Back to Community
How do I run a function from a different directory?
Profile icon
TOADER

When I Import a function from a file in another folder and run it nothing happens. Does anyone know how to fix this without putting them in the same folder?

Answered by RYANTADIPARTHI [earned 5 cycles]
View Answer
Voters
Profile icon
TOADER
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

i think you just do this.

from folder import file1 from folder.file1 import testdef print(testdef())

like that.
Maybe that works.

Profile icon
TOADER

@RYANTADIPARTHI You are correct, however you need to have the function return the variable before printing it.

Profile icon
RhinoRunner

the reason you got the error is because for is because you did:

from folder.file1 import testdef print(file1.text1) #wrong

since text1 is a local variable inside of testdef, and you imported testdef from file1, you need to do:

from folder.file1 import testdef print(testdef.text1)